2

How do I add a shortcode without plugins? I am going to add a shortcode that will give a count of views on a page.

4
  • 2
    What have you tried so far? Have you looked into this? – Howdy_McGee Apr 28 '20 at 14:49
  • 2
    You can add them in themes just like you can add them in plugins. – WebElaine Apr 28 '20 at 14:55
  • 1
    The shortcode portion is the easy part, first focus on the logic behind the counting of views on a page. Once that's done it's relatively easy to make it a shortcode. However, as a suggestion, instead of a shortcode, I'd recommend making it a template tag, that way you can just add it to the template for the post_type you want it to work with, and it'll show up on each page. That way you don't have to manually add the shortcode. – Tony Djukic Apr 28 '20 at 14:58
  • thank you, where can I find out more about template tags? – Nate Apr 28 '20 at 15:06
1

You'll probably need to modify this a bit to ensure things get placed where you want them but here's something I wrote that counts views on posts and displays the view counts for administrators:

    //ADD TRACKER FUNCTION TO ALL SINGLE VIEWS
    function custom_hook_tracker( $post_id ) {
        if( !is_single() ) return;
        if( empty( $post_id ) ) {
            global $post;
            $post_id = $post->ID;
        }
        custom_track_post_views( $post_id );
    }
    add_action( 'wp_head', 'custom_hook_tracker' );

    //ADD VIEW TRACKER
    function custom_track_post_views( $postID ) {
        $count_key  = 'custom_view_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 THIS LINE IF YOU USE THE TEMPLATE TAG
        if( is_single() && current_user_can( 'administrator' ) ) { //remove or adjust the '&& current_user_can( 'administrator' )' to modify who CAN see the counts
            //ALSO REMOVE THIS LINE IF YOU USE THE TEMPLATE TAG
            echo '<div style="position:absolute;top:2.75rem;width:auto;display:block;float:left;background:#FFF;border-radius:4px;box-shadow:#333 1px 1px 4px;padding:0.25rem 0.5rem;margin-left:-5px;color:#777;"><small>Views: '.$count.'</small></div>';
        }
    }
    //REMOVE PRE-FETCHING TO KEEP COUNTS ACCURATE
    remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

Ok, I couldn't just leave it - here's the template tag:

function custom_view_counts() {
  $count_key    = 'custom_view_count';
  $count        = get_post_meta( $postID, $count_key, true );
  echo '<div class="view-tracker"><small>Views: '.$count.'</small></div>';
}

Then to display it drop this in your theme files exactly where you want it to appear: <?php custom_view_counts() ;?>

You'll need to write css that address/styles/targets the .view-tracker class/div.

You can basically build your own plugin, or just drop all of the above into your functions.php.

7
  • 1
    Thank you, Tony. – Nate Apr 28 '20 at 15:11
  • 1
    I'm sorry I wasn't able to put together the template tag part for you but I've got a deadline to meet. Basically that last bit about if( is_single() && current_user_can() ) { - the line echoed after that could be placed into a template tag using the $count_key and $count at the top of the custom_track_post_views() function to create a new template tag that you can drop anywhere in your theme, style as you wish, etc. – Tony Djukic Apr 28 '20 at 15:14
  • 1
    Nathaniel, I decided to live dangerously and spent another 5 minutes putting together the template tag. I have updated my answer. – Tony Djukic Apr 28 '20 at 15:22
  • Thank you, Tony – Nate Apr 28 '20 at 17:29
  • So I put this in what file? – Nate Apr 28 '20 at 17:30
2

There is a whole section on the WordPress Codex on this: Shortcode API

[foobar]

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

With attributes:

[bartag foo="foo-value"]

function bartag_func( $atts ) {
    $a = shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts );

    return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );
2

Please check the official WordPress Shortcode API. You can create custom shortcodes easily without any Plugin.

Code Example:

// HOW TO EMBED         
// Shortcode: [new_shortcode]          

function create_newshortcode_shortcode() {
 // Do whatever you want          
}         
add_shortcode( 'new_shortcode', 'create_newshortcode_shortcode' );

Links you can help:

https://codex.wordpress.org/Shortcode_API

https://app.wp-monkey.com/generators/shortcode-generator

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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