• How would be possible to get a clean coding workflow to change hyperlink structure on post save. So lets say an author has been created a post with hyperlinks <a href="https://www.example.com"></a>, on save I would like to control if meets the hyperlink given conditions to be redirected or not. If the url has to be redirected than should change the structure of it <a href="goto.html?url=https://www.example.com"></a>. What I want to avoid is to not push the Author to give attention on link creation.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m not sure I’m fully understanding, but here we go anyway. ??

    If your main goal is to filter post content and look for links that need to be changed ( if they meet a certain set of criteria ) without the author knowing about it, then try this:

    In your functions file:

    function alter_links_in_posts( $the_content ) {
        // Do whatever you want to the content here:
    
        /*
         * Now anywhere you use the_content(), you'll get the
         * modified version you whipped up in this function.
         */
        return $the_content;
    }
    
    add_filter( 'the_content', 'alter_links_in_posts' );

    Shortcodes may also be applicable depending on what exactly you’re trying to do.

    Hopefully, this can get you started, at least. Just ask if any of this doesn’t make sense or I misunderstood you. ??

    Thread Starter deroccha

    (@deroccha)

    Thanks a lot for your feedback in the main time I have been fixing this issue, and mainly as you suggested. Just one more thing what I would like to know. Is it possible to make this without to include the filter function in the template file. Is it possible to make a plugin for this? I’m new to wordpress

    It’s definitely possible to make this a plugin. In fact, it’s a great idea.

    Here’s what you could do:

    <?php
    /*
      * Plugin Name: Filter Post Author Links
      * Plugin URI: https://codespanker.wordpress.com
      * Description: This plugin allow anchor tags in posts to be filtered/altered before output by the_content().
      * Author: Spencer Cameron - [email protected]
      * Version: 1.0
      * Author URI: https://codespanker.wordpress.com
    */
    
    function alter_links_in_posts( $the_content ) {
        // Do whatever you want to the content here:
    
        /*
         * Now anywhere you use the_content(), you'll get the
         * modified version you whipped up in this function.
         */
        return $the_content;
    }
    
    add_filter( 'the_content', 'alter_links_in_posts' );
    ?>

    Copy-paste this code into a file and name it whatever-you-want.php. Make sure you put this file in wp-content/plugins/. Once you do that, it will be available to activate from your plugins menu in WP admin. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Check Hyperlinks on post save’ is closed to new replies.