admin管理员组文章数量:1122846
I need to do check the inserted post for certain keywords and perform some functions depending on the keywords found.
is there a wordpress hook that executes "just before the post is inserted into the database" ? Or will i have to modify wordpress core ?
Also, i will need to prevent the post from being inserted into the database if some keywords are found. is this something that can be done with a hook ? Or will a core modification be needed ?
I need to do check the inserted post for certain keywords and perform some functions depending on the keywords found.
is there a wordpress hook that executes "just before the post is inserted into the database" ? Or will i have to modify wordpress core ?
Also, i will need to prevent the post from being inserted into the database if some keywords are found. is this something that can be done with a hook ? Or will a core modification be needed ?
Share Improve this question edited Mar 14, 2011 at 16:48 YD8877 asked Mar 14, 2011 at 16:40 YD8877YD8877 2132 gold badges3 silver badges6 bronze badges4 Answers
Reset to default 5Available actions:
- pre_post_update - Runs just before a post or page is updated.
- publish_post - Runs when a post is published, or if it is edited and its status is "published".
- save_post - Runs whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email.
- wp_insert_post - Same as save_post, runs immediately afterwards.
More info: Plugin API/Action Reference
Usage:
<?php
add_action('action_name', 'callback_name');
The hook is save_post:
add_action('save_post', 'dosomething'), 10, 2);
the hook is wp_insert_post_data
which as two parameters ($data, $postarr) and its a filter hook that fires before inserting the post into the database so:
add_filter('wp_insert_post_data','callback_function');
as for save_post
hook it happens after inserting the post to database.
Wordpress likes to do auto-saves / auto-drafts. There is no hook currently available to stop an insert based on specific post data.
However, you can stop the post from being published based on specific post data by using wp_die(). I have used this to check for duplicate SKUs on ecommerce sites.
In this example I am getting the "SKU" value from an ACF Field, but if you're not using ACF that's fine, just use print_r($_POST);
to view your post data, and go from there.
This example also checks to see if the post we are updating is a custom post type, you could also omit that if not required.
//Check if a SKU already exists, and display an error if it does
add_action('pre_post_update', 'tix_check_for_duplicate_sku', 1, 2);
function tix_check_for_duplicate_sku( $post_id, $data ) {
//Check if we are updating a product (custom post type) if not, just return
if (get_post_type() != 'tix_product') return;
$sku = $_POST['acf']['field_662e98fccf0da'];
if ($sku != "") {
$sku_check = new WP_Query([
'post_type' => 'tix_product',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => array($post_id),
'meta_key' => 'sku',
'meta_value' => $sku,
'fields' => 'ids'
]);
if ($sku_check->found_posts >= 1) {
wp_die('Duplicate SKU: '.$sku.' exists. All SKUs must be unique! Please go back and change it...', 'Duplicate SKU', array('back_link' => true));
}
}
}
本文标签: customizationWordpress hook before inserting post into database
版权声明:本文标题:customization - Wordpress hook before inserting post into database 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736305026a1932444.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论