问题
i have a custom field for source-links. The URL inserted in this custom field should get pinged once the post gets published. I know there is a dedicated trackback field but i want the URL from the custom field automatically added.
As far as i understand, $add_ping is exactly that. http://codex.wordpress.org/Function_Reference/add_ping
My problem is: i don't know where to add it. I imagine if i'd write this into my theme where i display the source-link, the source gets pinged everytime the post gets visited.
So whats the proper way to add an url to get pinged on publish?
To be clear: it is not a service that i want to get pinged. More like if you'd insert a link to post B into post A's content. Once post A gets published, post B (or its blog) gets pinged. I want to insert the link into a custom field of post A, instead of its content area.
回答1:
I think Felipe is right. You could try something like this:
function doCustomPing ($post_id) {
$uri_to_ping = get_post_meta($post_id, 'FIELDNAME', true);
add_ping($post_id, $uri_to_ping);
}
add_action ('publish_post', 'doCustomPing');
So every time a post if published (that includes if its edited) then a hook into publish_post will run the doCustomPing function. If you're putting this as part of a theme drop the above code in your functions.php file. Put the name if your custom field where it says FIELDNAME.
来源:https://stackoverflow.com/questions/14093772/wordpress-how-to-ping-url-from-custom-field-on-publish