DisplayPluginOption( 'arp_number_of_random_post' );
?>
DisplayPluginOption( 'arp_frontpage_position' );
?>
DisplayPluginOption( 'arp_text_title' );
?>
DisplayPluginOption( 'arp_keep_the_limit' );
}
function HTML_DisplayPluginOptionsListedBlock()
{
$optionsArray = $this->GetOptionsArray();
if( is_array( $optionsArray ) )
{
?>
| Option Name |
Option Type |
Option Value |
Option Description |
Option Values Array |
$optionValueArray )
{
?>
|
GetOptionType( $optionKey ) ); ?> |
GetOptionValue( $optionKey ) ); ?> |
GetOptionDescription( $optionKey ) ); ?> |
GetOptionValuesArray( $optionKey ) ); ?> |
Selects a defined number of random posts from the archive and shows them on the home page
Initialize( 'Advanced Random Post', '0.1', 'advanced-random-post', 'advanced-random-post' );
// Add all of the options specific to your plugin then register the options with the Wordpress core.
$myComboboxRP = array( '1', '2', '3', '4', '5' );
$myARP->AddOption( $myARP->OPTION_TYPE_COMBOBOX, 'arp_number_of_random_post', $myComboboxRP[0], 'How many random post i show ?', $myComboboxRP );
$myComboboxFP = array( '0','1', '2', '3', '4', '5' );
$myARP->AddOption( $myARP->OPTION_TYPE_COMBOBOX, 'arp_frontpage_position', $myComboboxFP[1], 'Random post position in the front page. Indexed from 0 (zero)',$myComboboxFP );
//$myARP->AddOption( $myARP->OPTION_TYPE_CHECKBOX, 'arp_keep_the_limit', $myARP->CHECKBOX_CHECKED, 'if unchecked then the random posts will be added to the number of posts already shown on the front page' );
$myARP->AddOption( $myARP->OPTION_TYPE_TEXTBOX, 'arp_text_title', 'Random Post:
%title', 'Text displayed in the title of random post. %title will be replace with original post title.' );
$myARP->RegisterOptions( __FILE__ );
$myARP->AddAdministrationPageBlock( 'block-description', 'Plugin Description', $myARP->CONTENT_BLOCK_TYPE_MAIN, array($myARP, 'HTML_DisplayPluginDescriptionBlock') );
$myARP->AddAdministrationPageBlock( 'block-options-displayed', 'Plugin Options', $myARP->CONTENT_BLOCK_TYPE_MAIN, array($myARP, 'HTML_DisplayPluginOptionsDisplayedBlock') );
$myARP->RegisterAdministrationPage( $myARP->PARENT_MENU_OPTIONS, $myARP->ACCESS_LEVEL_ADMINISTRATOR, 'Advanced Random Post', 'ARP Options Page', 'arp-plugin-options' );
}
function arp_core($posts){
global $wpdb, $wp_query, $paged;
if(!$wp_query->is_home || $paged > 1 || $wp_query->query_vars['paged'] > 1)//random post should be only on the front page
{
return $posts;
}
$my_posts = array();
$j = count($posts);//trick to keep the limit of posts per page
for($i = 0; $i <= $j; $i++){
$post = $posts[$i];
if($i == get_option('arp_frontpage_position') ){
$oldest_post = $posts[count($posts)-1]->post_date_gmt;//to select posts that are not on the page already
$now = current_time('mysql', 1);
//let's start with query
$query = "SELECT * FROM $wpdb->posts ";
$query .= " WHERE post_status = 'publish' AND post_date_gmt <= '$oldest_post' AND post_type = 'post' ";
$limit = get_option('arp_number_of_random_post');
$orderby = 'rand()';
$query .= "ORDER BY $orderby ";
$query .= " LIMIT {$limit}";
if($drafts = $wpdb->get_results($query)){
foreach($drafts as $draft){
$draft->post_title = str_replace("%title", $draft->post_title, get_option(arp_text_title) );
$my_posts[] = $draft;
$random_posts[] = $draft->ID;
}
//we don't want to exceed the limit of posts per page
//if(get_option('arp_keep_the_limit')){
// $j -= count($drafts);
//}
}
//for debug
//echo $query;
}
if($i < $j){
$my_posts[] = $post;
}
}
update_post_caches($my_posts) ;
return $my_posts;
}
// Add filter
add_filter('the_posts','arp_core');
?>