最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - How to customize save function event?

programmeradmin0浏览0评论

I'm trying to customize the save feature of a Wordpress theme, unfortunately I doesn't have the necessary knowledge on this so I need a bit help to do this in the right way.

Essentially the theme that I bought allow me to create a property, the property is stored in the post table as a normal post, but contains of course the theme taxonomies and other options.

When I create or update a property, I need to execute a custom function, but I have no idea where to place it.

When a property is added or updated I saw in the network console that this file is called:

http://localhost/wordpress/wp-admin/admin-ajax.php?action=oembed-cache&post=24987

How can I intercept the $_POST data passed to that file and send to a custom function?

I tried with:

function fpw_post_info( $id, $post ) {
    var_dump("hello world");
}
add_action( 'publish_post', 'fpw_post_info', 10, 2 );

but how can I see if the code works?

Kind regards.

I'm trying to customize the save feature of a Wordpress theme, unfortunately I doesn't have the necessary knowledge on this so I need a bit help to do this in the right way.

Essentially the theme that I bought allow me to create a property, the property is stored in the post table as a normal post, but contains of course the theme taxonomies and other options.

When I create or update a property, I need to execute a custom function, but I have no idea where to place it.

When a property is added or updated I saw in the network console that this file is called:

http://localhost/wordpress/wp-admin/admin-ajax.php?action=oembed-cache&post=24987

How can I intercept the $_POST data passed to that file and send to a custom function?

I tried with:

function fpw_post_info( $id, $post ) {
    var_dump("hello world");
}
add_action( 'publish_post', 'fpw_post_info', 10, 2 );

but how can I see if the code works?

Kind regards.

Share Improve this question edited Oct 7, 2019 at 15:50 WebElaine 9,8141 gold badge17 silver badges28 bronze badges asked Oct 7, 2019 at 15:08 sfarzososfarzoso 498 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

WordPress fires a number of different hooks when a post is (published / updated / saved). It's usually best to browse through the Codex to determine which hook fires when, and only when, you want to run your custom code. It's important to be aware that some of these hooks fire multiple times when you click a single button.

In most cases you can't run a var_dump(), so it's often helpful to add a test function on each hook, and in that test function, fwrite to a log file including the timestamp and which hook ran. That way you can see the order of things and also how many times each hook ran, and start narrowing down a single place to run your custom code.

One hook you might want to explore: save_post. This hook gives you access to the post ID, the full WP $post object, and a boolean for whether this is an existing post being updated.

So, to test out the save_post hook, you could start with something like

add_action('save_post', 'wpse_see_save_post', 20, 3);
function wpse_see_save_post($post_id, $post, $update) {
    // Open a log file and add to it ("a")
    $file = fopen(__DIR__.'/save-post-hook.log', 'a');
    // Get the current timestamp
    $time = date("Y-m-d h:m");
    // Write timestamp and message to file
    fwrite($file, "$time - save_post ran");
    fclose($file);
}

You might also want to log a few other hooks such as transition_post_status and post_updated - maybe even to the same logfile so you see the full order of things and what's available.

Once you know what order things are happening in, you can start accessing the data. For example:

add_action('save_post', 'wpse_see_save_post_data', 20, 3);
function wpse_see_save_post_data($post_id, $post, $update) {
    // Open a log file and add to it ("a")
    $file = fopen(__DIR__.'/save-post-data.log', 'a');
    // Get the current timestamp
    $time = date("Y-m-d h:m");
    // Write timestamp and message to file
    fwrite($file, "$time - save_post data includes\r\n");
    // access global $_POST array
    global $_POST;
    fwrite($file, print_r($_POST, true));
    fclose($file);
}

You'll likely notice that the $_POST data is empty on one run, but populated on another. So, you can add a conditional in your function like

if(count($_POST) > 0) {

to have your custom code only run when you know you have access to the data you need.

Generally speaking in the WordPress environment you can capture your $_POST data in the functions.php of your activated theme.

Then I often find quick and useful using this function, from time to time, to debug data and see what I’m receiving:

function write_log( $log )  {
  if ( is_array( $log ) || is_object( $log ) ) {
     error_log( print_r( $log, true ) );
  } else {
     error_log( $log );
  }
}

Putting it into your functions.php you might be able to:

function fpw_post_info( $id, $post ) {
    write_log($_POST);
}
add_action( 'publish_post', 'fpw_post_info', 10, 2 );

At this point you can see the passed data (and any other kind of data) in debug.log if you activate the debugging in WP. Read more https://codex.wordpress/Debugging_in_WordPress

发布评论

评论列表(0)

  1. 暂无评论