Feed to Post Filter: Make Post Titles Link Directly to the Original Article

By default, the post titles of posts imported by WP RSS Aggregator are linked to the single post on your site.

By adding the below filter they are linked to the original source with a click behavior that opens in the same tab. This means that on the blog index page of your site, each imported blog post, when clicked on, will link to the external original source's website.

NOTE: The ability to have these post titles open in a new tab or window is controlled by your theme, not WP RSS Aggregator.

Apply this Behaviour to All Imported Posts

Using the below code snippet will make all imported posts link directly to the original version.

add_filter( 'wprss_ftp_link_post_title', '__return_true' );

Apply this Behaviour to Selected Sources Only

Using the below code snippet will make the imported posts for selected feed sources link directly to the original version.

add_filter( 'wprss_ftp_link_post_title', 'link_my_post_titles' );
function link_my_post_titles() {
    // Return an array of feed source IDs. Replace 23, 49, 11 with your own feed source IDs.
    return array( 23, 49, 11 );
}

SEOPress Plugin Compatibility

When using the above filter, a possible side-effect is that sitemap.xml will also point to the original source rather than the local content. In order to avoid this from happening when using the SEOPress plugin, please use the below filter instead.

add_filter( 'wprss_ftp_link_post_title', function ( $link ) { 
    return get_query_var( 'seopress_cpt' ) === '';
} );

Further Examples

This filter can be modified to apply in different situations.

For example, the code snippet below checks if the post type of the imported post is job, and if it is, it sets the post to link to the original article from the title.

add_filter( 'wprss_ftp_link_post_title', 'link_my_post_titles' );

/**
* Returns TRUE (link to original post) if the post is of the post type 'job'.
* Returns FALSE otherwise (link to imported post on our site).
*/

function link_my_post_titles() {
    return get_post_type() === 'job';
}

In some cases, the post title links might not link externally when on the singular post view. If this is the case, apply the additional filter below.

if ( !function_exists( 'my_f2p_link_posts_to_external' ) ) {
   
     /**
     * This allows linking Feed-to-Post-imported posts to their external source using the
     * filter from here: https://kb.wprssaggregator.com/article/295-f2p-filter-make-post-titles-link-directly-to-the-original-article
     * even on a single post page.
     * The original method WPRSS_FTP::link_posts_to_external() only does this when
     * is_single() is false to prevent clashes with the post that is being currently
     * displayed. However, the posts that need to link to an external page may
     * be displayed in another loop.
     * Thus, another handler is hooked in, which is identical to the original,
     * except it does not check for is_single().
     *
     * @param string $url The original URL
     */

    function my_f2p_link_posts_to_external( $url ) {
            // If the id parameter was not passed, do nothing and return the title.
            if ( $url === NULL || get_post() === NULL ) return $url;


            // Get the feed source for the post
            $source = WPRSS_FTP_Meta::get_instance()->get_meta( get_the_ID(), 'feed_source' );
            // IF AN IMPORTED POST
            if ( $source !== '' ) {
                // Check whether the title is to be linked to the external, original post
                $filter_value = apply_filters( 'wprss_ftp_link_post_title', FALSE );
                // Get the permalink meta data for the post
                $permalink = get_post_meta( get_the_ID(), 'wprss_item_permalink', TRUE );

                // If the permalink is empty, return the regular WordPress post url
                if ( $permalink === '' ) return $url;

                // If the filter value is an array, check if the source ID is in the array
                if ( is_array( $filter_value ) ) {
                        $link_external = in_array( $source , $filter_value );
                }
                // If the filter value is not an array, check if it is TRUE or if it is the ID of the source
                else {
                        $link_external = ( $filter_value === TRUE || strval($filter_value) === strval($source) );
                }

                // If link_external is TRUE, return the permalink of the original article.
                // Otherwise, return the regular WordPress post url
                return ( ( $link_external === TRUE )? $permalink : $url );
            }
            else {
                return $url;
                }
}

add_filter( 'post_link', 'my_f2p_link_posts_to_external', 11 );

How do I add this to my site?

Follow the step-by-step instructions below to add this filter to your WordPress site.

  1. Copy the code you need from above.
  2. Go to your WordPress site's dashboard.
  3. Go to Plugins > Add New.
  4. Search for Code Snippets, then install and activate the plugin.
  5. Once installed and activated, go to Snippets in your dashboard menu.
  6. Click on Add New.
  7. Add a Title, which could be the title of this article.
  8. Paste the code you copied in step 1 to the Code section.
  9. Add a Description or Tags if you wish to do so. It is not required.
  10. Click on Save Changes and Activate to save the filter and activate it.
    1. Or click on Save Changes to save the filter and activate it later.
  11. Your action or filter is now stored and active on your site.

Still need help? Contact Us Contact Us