Linkback Your Xpost: A Simple WordPress Filter Function

Despite being a rather ancient WordPress plug-in as these things go (not updated in 5 years), one whose functionality for many users may have been superseded by and other “multi-posting” tools, Xpost by Jan Gosmann still works quite well. Since the latest updates to WordPress Core (4.4 as of this writing), even some annoying error messages have cleared themselves up.

I never researched those errors in detail, though I’m prepared to do so if they return, because, for someone like me who posts regularly at two different WordPress sites, Xpost is a time-saver, making it easy to transmit a post from one blog to another, and also automatically synchronizing edits from the original post to the new one. One further reason to use a plug-in like Xpost instead of depending on or other third-party applications is greater ability to customize. With Xpost, the customization I want to achieve is to add a note with every Xposted post linking back to the source, something that previously when cross-posting I would have to add manually. Achieving this objective turns out be quite easy, requiring a mere 25 or so lines of PHP code, which can be added to the Theme’s functions.php file.

Xpost can also be set, if desired, to transfer comments, though I haven’t used that function very much beyond some initial experiments. For that matter, synchronization of post content can get somewhat confusing, since every time you re-save the original post it will replace the target post, possibly overwriting any edits made on the latter in the meantime. The main purpose of this post, however, is not to describe Xpost in detail or how to set it up, but rather to provide a simple customization function, and in the process to explain for WordPress beginning developers how certain critical WP functions work.


We want the back-link, shown in the image below – “(Cross-posted at Ordinary Times)” – to be added automatically to all cross-posted posts.


Post with Automatically Added Link Back to Source Post


* Add XPost Source Link:
* Automatically places a formatted link back to the source of
* a post crossposted by the Xpost plug-in;
* illustrates simple content filter function.
add_filter( 'the_content' , 'add_xpost_info' );

function add_xpost_info( $content ) {

     global $post;

     if ( get_post_meta( $post->ID , 'xpost_original_posturl' ) ) {

     //Set the variables
     $original_post_url = get_post_meta( $post->ID , 'xpost_original_posturl' , true );
     $origin_name = get_post_meta( $post->ID , 'xpost_origin_name' , true );
     $title = get_the_title( $post );

     //Create the infobox
     $infobox = '<p style="text-align: right;"><em>(Cross-posted at ';
     $infobox .= '<a title="' . $title; $infobox .= ' at ' . $origin_name . '" href="'. $original_post_url . '"> ' . $origin_name . '</a>)</em></p>';


     return $infobox.$content;



So, taking it line by line, and setting aside the commented-out “title” heading:


Every time the content of a post is posted by WordPress, the `the_content` “filter” is called. So, anything added to the_content will be added to the post.

Many plug-ins use this filter, so it’s easy to run into log-jams, though more typically at the end of a post than at the beginning. When someone else’s plug-in or function is exploiting `the_content`, you’ll have to deal with order (often “priority”) of execution, which is set by number – the lower the number, the earlier the “firing” of the code. The default is “10” (which will be fine for my purposes). If you had, for example, a plug-in adding a “social share” box at the top of the content area of the post, or maybe a custom image icon or some such, then you might have to raise or lower the number in order to get the placement just right: If the other function was set at the default, 10, maybe you’d move it up by setting it at 9, or maybe you’d set yours at 11 or 111 or 453, as in `add_filter( ‘the content’ , ‘add_xpost_info’ , 453)`. If there aren’t any other functions firing at `the_content`, and, placing text or to be more precise HTML code at the beginning of the content area, you wouldn’t have to worry about it.

`add_xpost_info` is the title of the function that is being “called” by the filter function. So, in the latter’s two main arguments, the first being the call to `the_content`, `’add_xpost_info’` is a “callback.”

The function itself begins with the following line, repeating `add_xpost_info` and calling the `$content` variable.

function add_xpost_info( $content ) {

The variable `$content` will be available here, when filtering `the_content`. As you will see, `$content` passes through the function and picks up our additional content.

The line…

global $post;

…summons the global variable `$post`, which will be available wherever a post is being processed. `$post` holds the WordPress “Post Object,” which will normally be a relatively compact version of all of the data associated with the particular post. (Sometimes, you’ll have to use `set_up_postdata()` to access that additional data, but it will not be necessary here, since the only thing you need from the post will be its “ID” number.) `$post` is used in the following line:

if ( get_post_meta( $post->ID , ‘xpost_original_posturl’ ) ) {

To extract values (in this case the ID number) from an object, you use the `->` format.  The “if” conditional using the `get_post_meta()` function tests whether there’s any `xpost_original_posturl` “key” in “Post Meta”: Post Meta (short for post metadata) contains the data that WordPress will store in the database in association with a given post. When Xpost sends over a post from the source blog, the Infobox Widget functions send the metadata we are using in this function along with it. If `get_post_meta` does not find `x_post_original_posturl` on the post, it “returns false,” and the rest of the code will not be executed. ((You can see the key-value pairs when you bring up the newly transmitted post on your post edit screen, in the Custom Fields area, which is normally set to display:  If you can’t find it, check Screen Options up at the top, and be sure its checkbox is checked.)) If the` xpost_original_posturl` key has been sent over and therefore exists in Post Meta, then the if condition returns `true`, and the rest of the function will execute.

We will need only two of the values that Xpost sends over, the one for `xpost_original_posturl` and the name of the source blog, saved with the key `xpost_origin_name`. (Xpost also sends over the URL for the original blog, but we’re not using it.)

$original_post_url = get_post_meta( $post->ID , 'xpost_original_posturl' , true );
$origin_name = get_post_meta( $post->ID , 'xpost_origin_name' , true );

We add the argument `true` to each of the `get_post_meta()` statements here because otherwise `get_post_meta()` returns an array, and we don’t want an array, we want the single values.

Each of the two statements saves the value for one of the two keys in a variable. The next two lines use them to create the HTML that we’ll be using. It’s very straightforward PHP syntax, so I won’t explain it here.  As for the formatting, instead of hard-coding the style with the `text-align` and the `em` tag,  you might assign a CSS class or ID, but the below is quite adequate for my current purposes.

$infobox = '<p style="text-align: right;"><em>(Cross-posted at';
$infobox .= '<a href="'. $original_post_url . '" title="' . $original_post_url . '"> ' . $origin_name . '</a>)</em></p>';

The last line simply adds the new `$infobox` to `$content`, right in front of it (joined by the period).

return $infobox.$content;

So now, whenever `$content` is called on a cross-posted post, it will output the cross-posted infobox first.

If you don’t want the infobox to show up in certain places, or if you want different kinds of infoboxes for different kinds of content, and so on…

…you’ll need more code.


Someday someone may update Xpost or replace it with a similar function. The simple techniques described in this post can be applied in multiple situations, however – wherever the_content and filters like it are called.

Commenter Ignore Button by CK's Plug-Ins

Leave a Reply

Your email address will not be published. Required fields are marked *