Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have post views enabled and I'm attempting to make an options panel option to allow users to choose if they would like to display post views in each post. If they choose 'Yes," then they will be shown post views. If they choose 'No,' then they will not.

I'm hoping somebody can help me create the correct get_option to display post views based on what is chosen in the options panel.

What I tried to grab my post views, and it did not work:

<?php
if (get_option('to_post_views') != 'No' and function_exists('post_views_count'))
    post_views_count(); ?>

Without options enabled, I can grab my post views with the following:

<!-- Remember Post Views -->
<?php setPostViews(get_the_ID()); ?>

<!-- Display Post Views-->
<?php echo getPostViews(get_the_ID()); ?>

My function for post views:

// Post views
function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

My array for the options panel:

array( "name" => "Display post views?",
    "desc" => "Choose whether or not to display post views.",
    "id" => $shortname."_post_views",
    "type" => "select",
    "options" => array("Yes", "No"),
    "std" => "Yes"),
share|improve this question
    
Do you want a setting on the post-edit-screen when you creating a post and can choose if the post-count should shown or not? Or is it up for the user to choose if they could see the counter or not? –  Pontus Abrahamsson Dec 24 '12 at 9:37
    
I want a drop-down in my theme options panel as noted above. –  AndrettiMilas Dec 24 '12 at 16:42
    
do you have an options-panel in your theme now? –  Pontus Abrahamsson Dec 24 '12 at 16:43
    
When you say "an options panel option to allow users to choose if they would like to display post views in each post" do you thinking of a list with all posts and a option on all of them? –  Pontus Abrahamsson Dec 24 '12 at 16:51
    
Yes, I already have the options panel built. The code under "My array for the options panel" above builds the correct selection box. The code under "my functions" is correct to carry out the post views. Basically, I just need the getPostViews code to be incorporated with the array to display or not, but not the setPostViews - the new code and setPostViews will be embedded on every page. –  AndrettiMilas Dec 24 '12 at 17:05
show 7 more comments

1 Answer

up vote 2 down vote accepted

So this goes in your themes functions.php file. This adds a metabox on the right side of your editscreen within posts called "Enable Post Views on this Post": enter image description here

// Hook into WordPress
add_action( 'admin_init', 'add_custom_metabox' );
add_action( 'save_post', 'wpse_16722_save_post_views_value' );

/**
 * Add meta box
 */

function add_custom_metabox() {
    add_meta_box( 'enable_post_views', __( 'Enable Post Views on this Post' ), 'wpse_16722_enable_post_views', 'post', 'side', 'low' );
}

/**
 * Display the metabox
 */

function wpse_16722_enable_post_views() {
    global $post;
    $enable_views = get_post_meta( $post->ID, 'enable_views', true );

    ?>
        <p>
            <input type="checkbox" id="enable-views" name="enable_views" value="1" <?php checked( $enable_views, 1 ); ?> style="margin-right: 10px;">
            <label for="enable-views"><?php _e('Enable Post Views on this Post', 'domain'); ?></label>
        </p>

<?php
}

/**
 * Process the custom metabox fields
 */

function wpse_16722_save_post_views_value( $post_id ) {
    global $post;   

    if( isset( $_POST['enable_views'] ) ? $_POST['enable_views'] : '' ) {
        update_post_meta( $post->ID, 'enable_views', $_POST['enable_views'] );
    } else {
        delete_post_meta( $post->ID, 'enable_views' );
    }
}

/**
 * Get and return the values for the URL and description
 */

function wpse_16722_get_post_views_box() {
    global $post;
    $enable_views = get_post_meta( $post->ID, 'enable_views', true );

    return $enable_views;
}

It saves the value as metadata on your posts. The meta_key is "enable_views" and you can see it in your database under yourprefix_postmeta.

When you have the value saved on a post as true. You can simpley just check this on the post where you want the counter like this:

// Inside the loop add this 
// Get the values from "enable_views"
// If true add the views counter

$is_enabled = get_post_meta( $post->ID, 'enable_views', true );

if( $is_enabled ) {
    echo getPostViews( post->ID ); 
}

Update New 19.11

Only show views when options is "Yes" put this in your single.php

// Get the options
$options = get_option('to_post_views');

// Echo if options is Yes
if( $options == 'Yes' ) {
    echo getPostViews( get_the_ID() );
}
share|improve this answer
    
While this is a good solution for some, I'm seeking a way to enable post views in my theme options universally (across the entire site) not a specific check box on each post to enable. –  AndrettiMilas Dec 24 '12 at 16:41
    
Thank you Pontus! –  AndrettiMilas Dec 24 '12 at 18:12
    
Your welcome @siouxfan45 –  Pontus Abrahamsson Dec 24 '12 at 18:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.