Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Below is a simple shortcode I have so that in an article, I can simply link keywords to tag taxonomy pages in my site.

So if I were to wrap the term Web Service like this [taglink]Web Service[/taglink]

then the function below would replace the space with a - to make Web-Service which would be my tag taxonomy so I could then link to that page.

This...
[taglink]Web Service[/taglink]

Becomes this...
<a href="/tag/web-service" title="Web Service tagged articles">Web Service</a>

The code

function taglink_func($atts, $tag='') {
    $formattedTag = str_replace(' ', '-', $tag);
    $formattedTag = strtolower($formattedTag);
    return '<a href="/tag/' .$formattedTag. '" title="' .$tag. ' tagged articles">' .$tag. '</a>';
}
add_shortcode('taglink', 'taglink_func');

This solkution works fairly well but to improve performance even more, I would like to run this function when I create a new article and when I edit and article, so basicly only when an article is saved, I would like to run this function on any tags in the article and then replace the [taglink]Web Service[/taglink] in the actual database with the result.

So instead of running this on each page view, it will be ran on page save and simply replace the code in the database with the URL link.

Does anyone know how I can do this?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

If you want to modify content when the user saves a post, you'll want to hook in a function using the save_post hook.

Within that function, you can process registered shortcodes using get_shortcode_regex which will give you the shortcodes and attributes used in that post.

(However, if you want to process the content when the user saves, not when the content is displayed, then you probably don't need to register the shortcode to begin with. Just use the save_post hook and process the shortcode with your own regular expression.)

share|improve this answer
add comment

You can use the save_post and status_to_status and new_status_post_type hooks to accomplish this.

A much easier solution would be to use get_the_tags in your theme file instead. Or even wp_get_object_terms and use custom taxonomies if the tags you need to output differ from the actual tags of the post.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.