WordPress.org

Ready to get started?Download WordPress

Forums

How to Add a Hyperlink to Every Post on My Site at Once? (9 posts)

  1. agfreesafety
    Member
    Posted 1 year ago #

    I thought I would ask you guys, as I've been searching on Google with no luck so far...

    I have over 200 blog posts that I would like to add the same hyperlink text to, without having to manually go into each and every blog posts and add it.

    Is there a plugin or something I can use that I can use to do this?

  2. brasofilo
    Member
    Posted 1 year ago #

    Do this posts have anything in common?

    Where will you insert the hyperlink? If it is at the beginning or at the end, it would be easy. Else, not.

    Something like this would do:

    $args = array( 'post_type' => 'post', 'numberposts' => -1, 'post_status' => 'published' );
    $posts = get_posts( $args );
    foreach ( $posts as $post ) {
    	$po = array();
    	$po = get_post( $post->ID, 'ARRAY_A' );
    	$po['post_content'] = $post->post_content . '<br><a href="#">My hyperlink</a>';
    	//wp_update_post( $po );
    }

    Please note that wp_update_post is disabled.

    Backup of your database before anything!

    You can use the previous code without wp_update_post for testing purposes inside a custom template.

    To do the real thing, encapsulate the code above in a plugin:

    <?php
    /*
        Plugin Name: Insert Link in ALL Posts
    */
    register_activation_hook( __FILE__, 'insert_links_in_posts_activation_run' );
    
    function insert_links_in_posts_activation_run()
    {
        // put the first code here and enable wp_update_post
    }

    Put the code in a PHP file, upload to your plugins directory, activate.
    It will run only once on activation.
    Deactivate it.
    Check posts.
    I tested in a local install and works ok, but proceed at your own risk.

  3. agfreesafety
    Member
    Posted 1 year ago #

    Where will you insert the hyperlink? If it is at the beginning or at the end, it would be easy. Else, not.

    It will be at the end. How would I modify the code you provided (if at all) if I plan on putting it at the end?

    You can use the previous code without wp_update_post for testing purposes inside a custom template.

    How could I test whether or not it worked if wp_update_post is off? My knowledge of what wp_update_post does is minimal, so I apologize if that was a dumb question (I just Google'd it).

    Also, what is a custom template?

    I just backed up my database, I'm kind of nervous to do this, but I have to do what I have to do! :-)

  4. agfreesafety
    Member
    Posted 1 year ago #

    Also, how do you turn wp_update_post on?

  5. agfreesafety
    Member
    Posted 1 year ago #

    Does anyone have any additional insight on this topic that might help me? Thanks

  6. agfreesafety
    Member
    Posted 1 year ago #

    I was told by someone that I can just edit my post template to do this, by just inserting the html for the hyperlink after the content.

    They said I could use my post.php, but I don't have a file by that name in my theme.

    I have one entitled single.php, though, which is labelled "Single Post" in my wordpress admin panel.

    Is it true that I can just add my hyperlink to this file? If so, where?

  7. agfreesafety
    Member
    Posted 1 year ago #

    I ended up getting this to work by adding the desired hyperlink after <?php the_content(''); ?> in my single.php, and it looks fine, however I have one additional concern.

    As you can see in this screenshot (taken from this blog post), the text shows up at the end of the entire post, which is where I said I wanted it placed originally.

    The issue, is that it shows up beneath the "You Might Also Like" that's automatically placed at the end of every post via the LinkedWithin plugin.

    I would prefer for the text to show up before that section, if at all possible, as I think many visitors would fail to see it if it were placed in it's current position.

    Do you know of any way I can make it so that that text shows up above that "You Might Also Like" section?

  8. brasofilo
    Member
    Posted 1 year ago #

    Another approach (should have thought of that first):

    - use the plugin Search and Replace to remove the link from all posts.
    - place the following code in your theme's functions.php
    - adjust the $the_link
    - check the comments in the code (lines that start with //)
    - this filter enables the virtual manipulation of the_content

    // Giving the priority of 999 should make this print as last in the post
    add_filter( 'the_content', 'add_link_content_filter', 999 );
    function add_link_content_filter( $content )  {
        // This works for the template single.php
        // see http://codex.wordpress.org/Conditional_Tags
        if( !is_single() || is_admin() ) //is_admin prevents this filter in the backend
            return $content;
    
        $the_link =  '<a href="http://google.com">Google</a>';
        $content =  $content . $the_link;
    
        return $content;
    }
  9. agfreesafety
    Member
    Posted 1 year ago #

    I would like to accomplish the same thing with this site.

    I want to add some text that automatically shows up at the end of blog posts.

    I just compared my single.php for the new site with the one referenced above on our other website, but the two single.php's seem to be configured differently, therefore I'm not sure where (or if I can, even) place the desired text in the single.php for the new site.

    How would I go about this with the new site? Can I still use the single.php?

Topic Closed

This topic has been closed to new replies.

About this Topic