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 WordPress.com 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 WordPress.com 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.

THE OBJECTIVE

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

cross-posted_note

Post with Automatically Added Link Back to Source Post

CODE

EXPLANATION

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

add_filter('the_content','add_xpost_info');

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.1 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.)

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.

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.

CONCLUSION

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.

Notes:

  1. 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. []

WordPresser
Home Page  Public Email  Twitter  Facebook  YouTube  Github   

Writing since ancient times, blogging, e-commercing, and site installing-designing-maintaining since 2001; WordPress theme and plugin configuring and developing since 2004 or so; a lifelong freelancer, not associated nor to be associated with any company, publication, party, university, church, or other institution.

Posts in this series

Commenter Ignore Button by CK's Plug-Ins

Leave a Reply

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

*

Related

Noted & Quoted

TV pundits and op-ed writers of every major newspaper epitomize how the Democratic establishment has already reached a consensus: the 2020 nominee must be a centrist, a Joe Biden, Cory Booker or Kamala Harris–type, preferably. They say that Joe Biden should "run because [his] populist image fits the Democrats’ most successful political strategy of the past generation" (David Leonhardt, New York Times), and though Biden "would be far from an ideal president," he "looks most like the person who could beat Trump" (David Ignatius, Washington Post). Likewise, the same elite pundit class is working overtime to torpedo left-Democratic candidates like Sanders.

For someone who was not acquainted with Piketty's paper, the argument for a centrist Democrat might sound compelling. If the country has tilted to the right, should we elect a candidate closer to the middle than the fringe? If the electorate resembles a left-to-right line, and each voter has a bracketed range of acceptability in which they vote, this would make perfect sense. The only problem is that it doesn't work like that, as Piketty shows.

The reason is that nominating centrist Democrats who don't speak to class issues will result in a great swathe of voters simply not voting. Conversely, right-wing candidates who speak to class issues, but who do so by harnessing a false consciousness — i.e. blaming immigrants and minorities for capitalism's ills, rather than capitalists — will win those same voters who would have voted for a more class-conscious left candidate. Piketty calls this a "bifurcated" voting situation, meaning many voters will connect either with far-right xenophobic nationalists or left-egalitarian internationalists, but perhaps nothing in-between.

Comment →

Understanding Trump’s charisma offers important clues to understanding the problems that the Democrats need to address. Most important, the Democratic candidate must convey a sense that he or she will fulfil the promise of 2008: not piecemeal reform but a genuine, full-scale change in America’s way of thinking. It’s also crucial to recognise that, like Britain, America is at a turning point and must go in one direction or another. Finally, the candidate must speak to Americans’ sense of self-respect linked to social justice and inclusion. While Weber’s analysis of charisma arose from the German situation, it has special relevance to the United States of America, the first mass democracy, whose Constitution invented the institution of the presidency as a recognition of the indispensable role that unique individuals play in history.

Comment →

[E]ven Fox didn’t tout Bartiromo’s big scoops on Trump’s legislative agenda, because 10 months into the Trump presidency, nobody is so foolish as to believe that him saying, “We’re doing a big infrastructure bill,” means that the Trump administration is, in fact, doing a big infrastructure bill. The president just mouths off at turns ignorantly and dishonestly, and nobody pays much attention to it unless he says something unusually inflammatory.On some level, it’s a little bit funny. On another level, Puerto Rico is still languishing in the dark without power (and in many cases without safe drinking water) with no end in sight. Trump is less popular at this point in his administration than any previous president despite a generally benign economic climate, and shows no sign of changing course. Perhaps it will all work out for the best, and someday we’ll look back and chuckle about the time when we had a president who didn’t know anything about anything that was happening and could never be counted on to make coherent, factual statements on any subject. But traditionally, we haven’t elected presidents like that — for what have always seemed like pretty good reasons — and the risks of compounding disaster are still very much out there.

Comment →
CK's WP Plugins

Categories

Extraordinary Comments

CK's WP Plugins