Post-to-Post Links II Now at WordPress.org

Post-to-Post Links II is a plugin for creating links to other posts, pages, tags, or categories on your WordPress site, and it’s now available for download at wordpress.org. I also created Post to Post Links II error: No post found with slug "post-to-post-links-wordpress-plugin".

There are a couple improvements with this version:

  • Now you can use it with tag slugs and tag IDs too
  • Fixed a bug with using category slugs

As explained in the readme file, it unfortunately can’t interpret the tags from Scott Reilly’s old “Easy Post-to-Post Links” plugin. I used his plugin heavily, and if you want to convert your old tags for use with my new plugin, you can. Doing so requires 1. you to be comfortable running a SQL query and 2. that you consistently used only post slugs or only IDs with Scott’s plugin. If you used IDs in some tags and slugs in others, then you can’t make the conversion (as there’s nothing in the syntax of Scott’s tag itself that indicates whether you’re linking by ID or slug).

These are the steps:

  1. Make a backup of your WordPress database, or at least the wp_posts table. This is very important, in case you make a mistake in the SQL.
  2. Run this SQL statement if you always used slugs for your links:
    update wp_posts set post_content = replace(post_content,'[post=','[p2p type="slug" value=');

    Or run this SQL statement if you always used IDs for your links:

    update wp_posts set post_content = replace(post_content,'[post=','[p2p type="id" value=');

I use the “text” attribute the same way Scott did, so that portion of the tag will carry over seamlessly in the conversion.

22 Comments

  1. bw May 19, 2009

    Would it be possible to extend this plugin to accept codes for creating other types of href links?
    For example, assume that WP_URL is url for full wp path (may be in a subdirectory), and WP_DOMAIN is the domain name, then it seems like one could easily extend this plugin to convert input like:
    [code:dir1/image.jpg this is text] to href=WP_URL/dir1/image.jpg
    and
    [code:/dir1/image.jpg this is link test] to href=WP_DOMAIN/dir1/image.jpg

    I will be trying this out, so could possibly submit patch too, if needed.

  2. Mike May 20, 2009

    Hi bw – could you explain a little more the reason for what you’re suggesting? My intention with this plugin is to let you make internal links that won’t break if you change your permalink structure. Image tags and links to images aren’t affected by WordPress’ permalink structure.

    It looks like what you want to do is dynamically allow for changes to the physical location of the WordPress installation itself. That strikes me as something that would only ever change for a very small number of people, and if it did change, could be fixed with a single search-and-replace across the blog content.

  3. bwooster May 20, 2009

    Yes – that is exactly what this is for – when I move a site, or change domain names, or change directory structure.
    Drupal has many plugins that do this, and I’m considering a move from drupal to wp, so was missing this functionality. It is also time saving to use this for full external web links – [code:http://cnn.com cnn] is easier than typing in href etc or even using the UI tools that break the flow when one is typing.

    Not saying this is anything that post-to-post would be right place to do it, but just looking around right now.

  4. Mike May 21, 2009

    Thanks for explaining. I’ll need to think about this. I see the value of what you’re suggesting, but I’m not sure my plugin is the right place for it. One thing to point out is that the officially supported syntax for WP shortcodes is more verbose than your examples. It would be something like [quicklink url=”http://cnn.com” text=”cnn”] so it might not provide the efficiency with typing that you’re looking for (it’s as much work as just typing a regular link tag). It’s possible to make up your own shortcode format, but then there’s more work to do in terms of making sure it works correctly in RSS feeds, search results, etc.

  5. Duane Douglas May 25, 2009

    the shortcode text attribute doesn’t show up in my feed. i guess this is because of how the plugin’s shortcode works. is there an easy way to get the text to show up in my feed? thanks.

  6. Mike May 26, 2009

    Hi Duane – I’m using WordPress’ add_shortcode syntax, and it’s creating links correctly in my feed. Are you using the latest version of WordPress (2.7.1)?

  7. tdrath July 6, 2009

    It would be even better if you could combine this with the pop-up listing of posts and pages that is in this plug-in:

    http://wordpress.org/extend/plugins/link-to-post/

    That way, you don’t have to stop editing and find another window to look up the post ID.

  8. Mike July 7, 2009

    Hi tdrath – that’s a good idea, thanks for letting me know. I haven’t seen that plugin before – it looks like it’s a new one in the plugin repository. I’ll look over the code and maybe get in touch with the plugin author since our plugins are similar.

  9. Ben July 21, 2009

    Hi Mike. Thanks, first of all, for what is a really useful and elegantly designed plugin.

    I’m using WP Remix — I know, I know… blech to the nth power, and believe me, I’m sorry, and I won’t be using it again. — but right now I’m using WP Remix which doesn’t (always/sometimes… who knows) play very nicely with your plugin. That is, sometimes, depending on the page and other mystery information, inserting the shortcode gets me a link, and other times I get to actually read the shortcode on the page… Woot!

    Point is – could you tell me what the “direct” php call to the make-a-link function is…

    To unpack that:

    I’m guessing (assuming) that by activating the P2P plugin in my WordPress control panel, I’m telling WP that if it sees any php it should include wp-contentpluginspost-to-post-links-ii in the places it looks to figure that out.

    (If that’s deeply incorrect, then I may be back here for another go-round.)

    Further, I can (sort of) tell WP Remix to read a certain bit as direct php code.

    So I want to be able to edit my page to include:

    (Note, the [php] tag is how WPRemix knows that the included text should be treated as php)
    [php]setLink(“slugValue”, “slug”, “Text of my link”); [/php]

    or whatever the right call is.

    I *think* setLink should be it, in which case I’ve got a problem with getting WPRemix to *understand* the php call… but I’d like to be sure.

    Thanks for your help. Looking forward to hearing from you.

    –Ben

  10. Mike July 21, 2009

    Hi Ben – you’ve almost got it. I have not tried this, but this should work – for example:

    setLink("type" => "slug", "value" => "hello-world", "text" => "my hello world post");

    I’m using the current standard WP shortcode technique – I haven’t tried WP Remix, but I’m surprised it would mess with shortcodes.

  11. Ben D. Manevitz July 21, 2009

    First of all – Thanks for your help with the function call. I’m not there yet, but that was the sort of fundamental mistake that would have stymied me for a while.

    Second – follow up: It seems that (with P2P Active) I can call function that exist in Post2Post.php, but not(?) within the Class defined therein.

    I tinkered a little and put in two functions, one above the class definition (called “insult”) and one within it (inside-insult), just so I could go in baby steps.

    I can call insult(); no problem.

    But when I try to call Post2Post::inside-insult(); I get yelled at.

    I’ve poked about a bit online and it seems that I might be calling the inside function incorrectly. I may need to do something with $this or pointers (->) or something, but I can’t figure it out.

    I know you’re not my personal php tutor, but if you could give me a hint on this, it’d be much appreciated.

    –Ben

  12. Ben D. Manevitz July 21, 2009

    And… while we’re on the subject…

    [RANT]
    WP Remix seems to mess with just about everything. It looked so much like _exactly_ what I was looking for when I got it, and it’s turned out to be so _exactly *not*_ what I was looking for once I started trying to use it.

    On the plus side (read sarcasm), I’m learning tons about CSS and PHP that I never expected to. Or really wanted to. I’m already anticipating a switch to either THESIS or something else (or most likely, a professional designer), but (ironically) I can’t afford the learning curve on a new product/platform right now.

    But… Grrrr! WP Remix is incredibly unsatisfying and frustrating and I wouldn’t recommend it to anyone, for any reason, at any time.

    [/RANT]

  13. Mike July 21, 2009

    Sorry, the syntax I gave you was missing the class name:

    Post2Post::setLink("type" => "slug", "value" => "hello-world", "text" => "my hello world post");

    But you said you tried something similar and it didn’t work… but it should work. The Post2Post class contains only static methods, which means you don’t need to say “new” or use -> in the method call.

    If WP Remix is blocking/ignoring the shortcode in some cases, perhaps it is blocking access to the plugin altogether. To confirm, you could try temporarily switching themes, and use my code example in the sidebar (I haven’t tried this either, but I believe setLink will work in the sidebar).

  14. Ben D. Manevitz July 21, 2009

    I’m afraid to switch themes, because of all the tweaking I’ve had to do to make WPRemix even a little useful for my purposes — I’m sure WPRemix will find a way to $#&% me when I switch back. I could do a backup and restore, but I just did a few tests… and it seems I *can* get to the plugin…

    I tweaked Post2Post.php to include a few simple functions, both inside and outside of the Class definition. I can call them from the page and get the expected results.

    I can pass multiple parameters, get it to go inside the Class, as in: Post2Post::function(param1, param2);

    But as soon as I try to do that funky array variable assignment thing, it chokes.

    I thought it might be technical syntax, so I tried it with different variations of double and single quotes, minus-greaterThan, equal-greaterThan, minus-space-slash-greaterThan, etc…

    No joy.

    I looked around some more and it might be a question of nested shortcodes – It’s not clear, because while some of the WPRemix stuff looks like shortcodes, I can’t find the function definitions anywhere (even with qgrep and some clever – if I say so myself – batch file writing).

    I FINALLY figured out a kludge which seems to work, partially…

    In case you get anyone else asking about this, here you go:
    Most if not all of the WP Remix pages have a quasi-shortcode call to [REMIX] at the top. Wherever you want your link, you have to temporarily exit the remix umbrella… so you put in

    [/REMIX][p2p value value value][REMIX]

    As far as I can tell it doesn’t break the pages. I’m not 100% on what it does to formatting.

    It doesn’t work in the sidebars (or, presumably the headers). Those pretend to be more php-like, so the function calls should work, but they don’t.

    So… all together now… WP Remix sucks.

    But the P2P plugin still rocks.

    –Ben

  15. Mike July 21, 2009

    Hi Ben,

    I’m sorry, I guess I’m having a off-day. I made another mistake in the syntax I gave you. Third times a charm:

    Post2Post::setLink(array("type" => "slug", "value" => "hello-world", "text" => "my hello world post"));
  16. Ben D. Manevitz July 21, 2009

    No, ‘scool. It’s definitely a WP Remix problem.

    I actually tried the parentheses variations, since I know that sometimes arrays can get squiggly with that.

    I event tried:

    “slug”,
    ‘type’ => “contact”,
    ‘text’ => “Contact Us”,
    ‘attributes’ => null);
    Post2Post::setLink($mlf_temp); ?>

    Which I thought was exceedingly clever. But no joy. I don’t get an error, I just get a null return value.

    Does the single/double quote make a difference? what about the => versus the -> ?

    –Ben

  17. Ben D. Manevitz July 21, 2009

    Okay – that last one got messed up, because I threw in a pre(formatted) tag set… sorry.

    point is, I tried:

    “slug”, “type” => “contact”, “text” => “Contact Us”, “attributes” => null);

    Post2Post::setLink($mlf_temp); ?>

    And that still didn’t work.

  18. Ben D. Manevitz July 21, 2009

    Yeah- okay – I’m a spaz. I tried defining an array called $mlf_temp with the various “attrib” => “value” and then passing $mlf_temp as the parameter.

  19. Scott Granneman January 20, 2010

    I’m a bit confused – when I surround the shortcode with HTML paragraph tags, when the page is actually processed & rendered by WordPress, the HTML tags surrounding the shortcode are removed! Any idea why this is, or how to fix it? Tks!

  20. Mike January 21, 2010

    Hi Scott – that sounds bizarre. Just to make sure, are you using the HTML editor? And what happens if you don’t put in the paragraph tags? WP should automatically add paragraph tags when display any text that has two link breaks before it.

  21. Blaze April 29, 2010

    Hi – great plugin, but I’ve got one question:
    is there any possibility to add post to post link with an internal anchor? I’ve made section on page using and I can’t use p2p plugin to go to that section from other page.
    Thx for rply.

  22. Mike May 3, 2010

    Hi Blaze- it can’t do that, but it’s an excellent suggestion. I’ll include it in the next version.

Comments are Disabled