WordPress.org

Ready to get started?Download WordPress

Forums

Creating plugin, not showing up where I want it. (5 posts)

  1. jmayoff
    Member
    Posted 2 years ago #

    Hi, I'm just newly starting out, writing my first plugin. I'm trying to have something (Google +1 button) show up at the bottom of every post. I want it to appear below the posts, but only when the post is displayed in full on its own page.

    Here's my plugin's code:

    class plusOnePutter{
    
    	// This function echoes the code necessary for the header or footer
    		public function	addToHead(){
    			echo '<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>';
    		}
    
    	// This echoes the code necessary to display the actual button
    		public function addButton($content){
    			return $content . '<br /><g:plusone></g:plusone>';
    		}
    
    }
    
    $myPlusOnePutter = new plusOnePutter();
    
    if (is_single){
    	add_action('wp_head', array($myPlusOnePutter, 'addToHead'));
    	add_filter('the_content', array ($myPlusOnePutter, 'addButton'));
    }

    Problem is, the plugin runs after every bit of content is displayed and not just on single posts. Perhaps, I'm misunderstanding the meaning of is_single?

  2. jmayoff
    Member
    Posted 2 years ago #

    I just noticed an error, which I've fixed and now the button is not showing up anywhere.

    I changed:

    if (is_single){
    to
    if (is_single()){

  3. digibucc
    Member
    Posted 2 years ago #

    i did something similar, though not by creating a plugin, and not with the same neatness that you did. however, i better understood it compared to how well i understand your method.

    for troubleshooting, make your is_single code do something simple, like echo hello. then you can be sure it is working at all.

    my method for adding something in the body after the content was such:

    function add_post_content($content) {
    	if ( is_single() ) {
    	$contemp = $content;
    	$content = '[gallery]';
    	$content .= $contemp;
    }
    	return $content;
    }
    add_filter('the_content', 'add_post_content');

    i added the "[gallery]" shortcode, to be sure it showed properly on
    every page. note, i re-arranged it though, so the shortcode came before the content. to do yours, i would do this:

    function add_post_content($content) {
    	if ( is_single() ) {
    	$gplus= '<a href="googleplus.com"><img src="gplus.jpg></a>';
    	$content .= $gplus;
    }
    	return $content;
    }
    add_filter('the_content', 'add_post_content');

    note, i didn't know the link/image so you'd want to correct that.

  4. jmayoff
    Member
    Posted 2 years ago #

    Thanks for the reply.

    Mine does essentially that, except I want to check to see that I'm on a single post's page BEFORE I get to the function (not within the function).

    I think I may be misunderstanding when is_single() returns TRUE.

    From wordpress.org

    is_single();
    // When any single Post page is being displayed.

    To my mind, that should ONLY be true when the post is being displayed on its own page (usually single.php) and NOT on the page that shows all the blog posts.

    Also, for my own sanity, although I knew it must be true, I checked in_the_loop(), which as I suspected was TRUE.

  5. digibucc
    Member
    Posted 2 years ago #

    that is correct.

    as i said, use is_single in a simple matter, just to check where
    it shows before testing your gplus code.

    if ( is_single() ) {
    echo 'only on a single post';
    }

    also, i would bet that if i simply move the is statement
    mine will still working, meaning there is something else
    we are missing.

Topic Closed

This topic has been closed to new replies.

About this Topic