Trigger a Link once a new post is Published - wordpress

Is there Anyway to trigger a link once new post is Publish ?. My goal is to use it to ping sitemap
http://google.com/ping?sitemap=http://www.example.com/my_sitemap.xml
I simply don't want to set cron job for this. Please Gurus in the house is there anyway I can use theme function to achieve this ? I am new to wp coding Please.

From the Wordpress Plugin API:
publish_post is an action triggered whenever a post is updated and its new status is "publish"
So in your theme's functions.php, write your function to call the google url, then use the add_action function, with publish_post as the hook, and your function's name as the callback.

function test()
{
write code here.....
}
add_action( 'publish_post', 'test' );

Related

Running PHP code before loading template in Wordpress

Since I've only started using Wordpress recently, I'm still trying to figure how to run PHP code prior to loading the template for a particular post.
I'm talking about code which would be executed in the contoller class in a MVC structure.
Obviously it's tempting to stuff it in the page's template file, but I'm sure this won't be exactly qualified as best practice.
Any suggestions on this matter? Many thanks.
The actions(list of executing) on front part WordPress:
muplugins_loaded
registered_taxonomy
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
load_textdomain
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user
init
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_styles
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_clauses
posts_selection
wp
template_redirect
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
get_search_form
loop_start
the_post
get_template_part_content
loop_end
get_sidebar
dynamic_sidebar
get_search_form
pre_get_comments
wp_meta
get_footer
get_sidebar
wp_footer
wp_print_footer_scripts
admin_bar_menu
wp_before_admin_bar_render
wp_after_admin_
bar_render
shutdown
So, if You need some magic without all core functions, You can put your code into own mu-plugin for example, and it will execute on start(muplugins_loaded action).
For basic functionality and with theme functions - wp_loaded, etc. see the list above.
Usual, uses init action(cause WordPress is fully loaded, but without header and other stuff), example:
add_action( 'init', 'my_func' );
function my_func() {
// Write some code here...
}

how to fire a function after the post is created or edited

I require to insert the recently edited or inserted into another website. I tried the wp_insert_post action hook. But it execute more times 4 or 5 times. Please suggest me the idea. Thank you
publish_post doesn't work for custom post types, the correct hook (action hook) is publish_{$custom_post_type}. You should use add_action() as this is an action hook.
Or use wp_insert_post()
publish_post
Use "publish_post" hook which use to trigger after edit / add post in wordpress.

wordpress: can't get do_action to work with custom function

I'm using woocommerce and on functions.php i've hooked my function to an action.
add_action('my_calculate_shipping','calculate_shipping_2');
function calculate_shipping_2() {
echo "test";
}
Now on cart-shipping.php i use do_action('my_calculate_shipping'); and test isn't displayed. I've used function_exists and it returns false.
How do i solve this?
The reason is plugins are loaded before theme,
Ref: WordPress Code Flow
You can wrap your add_action code in some hook which is called in upper level.

delete_post hook in Wordpress not working

add_action( 'delete_post', 'test_function' );
function test_function(){
echo "Hello!";
}
The "Hello!" isn't showing up when I delete a post (It is a custom post-type, but that shouldn't matter right?). How do I debug this?
EDIT: I can't put that code in any front-end files like header.php or index.php because I won't be able to view the output when I delete a post from the back-end. What's the best way to tackle this?
Thanks
Try doing the following to see if you are reaching your filter. I tested this here with 3.2.1 and it works fine for me.
function test_function(){
die('deleted post');
}
this action will not run until you delete the post from the trash.
If you want it to run when you move it to the trash the action is 'trash_post'.

wordpress - catching save event with a plugin

i'm writing my first plugin for wordpress and i have some doubts regarding the hooks.
So i want an action to be executed when admin saves a post, and i'm using (inside a class):
add_action( 'save_post', array($this, 'save_post'));
function save_post(){
global $wp_query;
var_dump($wp_query);
}
the problem is that it prints the global variable when admin opens the "create a new post" and it doesn't when the user saves a post.
I want it to happen the other way around, but i can't find anything in the docs, and i'm completely alone here.
Any help? thanks
I think you're looking for the 'publish_post' action. There's also a 'draft_post' if you need it.

Resources