WordPress.org

Ready to get started?Download WordPress

Forums

How to hard code social media icons to end of posts/pages? (8 posts)

  1. Shan
    Member
    Posted 2 years ago #

    I've been searching for tutorials for this, but can't find any. I'm trying to reduce my plugins and since I currently need two separate plugins to have the social media icons the way I want them, I thought hard coding the ones at the end of posts/pages would be easiest to accomplish.

    Anyone know of a tutorial for this?

  2. vtxyzzy
    Member
    Posted 2 years ago #

    You probably don't need a plugin. Create a filter on the_content and append the proper HTML in it.

    Your filter could be as simple as this (UNTESTED):

    function my_function($content){
       $my_text = "<p>Here is more content.</p>";
       return $content . $my_text;
    }
    add_filter('the_content', 'my_function');
  3. Shan
    Member
    Posted 2 years ago #

    Thank you.

    What file would I add this to and how do I make sure the icons appear at the end of the post after my signature? Could I use the code just as you have posted it after adding my HTML or are there more changes I would need to make?

  4. vtxyzzy
    Member
    Posted 2 years ago #

    The code goes in your functions.php, but be careful! If you have any errors in functions.php it will make your site unusable and you will need to use ftp (or a File Manager from your hosting service) to correct it.

    Whether it appears before or after your signature depends entirely on your theme and how the signature is added.

    If your signature is added by a plugin or a widget, can you put the HTML for the social media icons with it?

  5. Shan
    Member
    Posted 2 years ago #

    My siganture is hard coded in the functions php file because the plugins kept screwing up on me.

    The code is below:

    add_filter( 'the_content', 'add_signature', 1 ); 
    
    function add_signature($text) { 
    
    	global $post; 
    
    	if($post->post_type == 'post') 
    
    		$text .= '<div class="signature"><center><img src="http://lastshredsofsanity.com/wp-content/uploads/2012/02/ShanSig.png" alt="signature" /></center></div>';
    
    	return $text; 
    
    }

    My theme is WP-Ellie. You can see my site here: http://lastshredsofsanity.com

  6. vtxyzzy
    Member
    Posted 2 years ago #

    Then the icons code could just be added to that function, but it looks like you would need to test for both 'post' and 'page' page_type before adding it.

  7. Shan
    Member
    Posted 2 years ago #

    Yeah, what you said is all Greek to me. LOL I am not a good coder at all.

  8. vtxyzzy
    Member
    Posted 2 years ago #

    I can't tell you exactly how to insert your code because it may have things in it that would change the way it needs to be inserted. You can try using the code below, but remember that if there is an error, you will need to use ftp to correct it.

    Add this under the global $post; line:

    $extra_text <<<EOTEXT
    your extra code replaces this line
    EOTEXT;

    Be sure that the 'EOTEXT;' line starts at the left margin. It won't work otherwise.

    Then, insert this code as explained below:

    if ($post->$post_type == 'post' || $post->post_type == 'page') $text .= $extra_text;

    If you want your extra text before your signature, insert that line before the existing if($post->post_type == 'post') line.

    If you want the extra text after the sig, put that line just before the 'return $text;' line.

    If you would like to be sure that this will not cause errors, put the code for the extra text in a pastebin and post a link to it here and I will check it for you.

Topic Closed

This topic has been closed to new replies.

About this Topic