I am building mini-plugin, and i'm trying to update the wp_post table content of a post item.
My plugin add a input field in post edit.php page and changing the post_mime_type of the post in the database.
but since i am doing this in add_action( 'save_post', ..) i can`t use wp_update_post() because its doing a loop.
the relevante code is here:
add_action( 'save_post', 'member_manager_save_extras' );
function member_manager_save_extras($post_id){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
else if( !isset( $_POST['meta_box_nonce'] ) ||
!wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) )
return;
else if( !current_user_can( 'edit_post' ) )
return $post_id;
else{
$post = get_post($id) ;
$post -> post_mime_type = "sss" ;
wp_update_post( (array) $post ) ;
}
}
so how can i update the post_mime_type in another way?
The most straight forward way to achieve this would be to remove the action and re-add it again, before and after you call wp_update_post, respectively:
add_action( 'save_post', 'member_manager_save_extras' );
function member_manager_save_extras($post_id){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
else if( !isset( $_POST['meta_box_nonce'] ) ||
!wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) )
return;
else if( !current_user_can( 'edit_post' ) )
return $post_id;
else{
$post = get_post($id) ;
$post -> post_mime_type = "sss" ;
// Remove the action
remove_action( 'save_post', 'member_manager_save_extras' );
// Perform any update that uses the save_post hook
wp_update_post( (array) $post ) ;
// Add it back again
add_action( 'save_post', 'member_manager_save_extras' );
}
}
Related
I'm trying to add or update two post meta on saving a post. Add if it's a new post and update if existing, but this function creates four in the database instead of two meta.
add_action( 'save_post', 'add_rewards');
global $WCFM, $WCFMmp;
function add_rewards ($product_id){
if($product_id){
$post_type = get_post_type($product_id);
if($post_type == 'product'){
$product = wc_get_product( $product_id );
$reg_price = $product->get_regular_price();
$sal_price = $product->get_sale_price();
$pric = $product->get_price();
add_post_meta($product_id,'main_reward', $reg_price);
add_post_meta($product_id,'sub_reward', $sal_price);
}
}
}
As it explains in the save_post manual, you should use save_post_{$post->post_type} which minimizes the save_post calls on other post types. It's also a good idea to check for autosave.
Also, if you use update_post_meta instead of add_post_meta you'll end up with only one instance of each. As it explains in the manual for that function, it says:
If the meta field for the post does not exist, it will be added and its ID returned.
Can be used in place of add_post_meta().
add_action( 'save_post_product', 'so71077799_add_rewards', 99, 1 );
function so71077799_add_rewards( $product_id ) {
// Check to see if we are autosaving, if so, exit.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['_regular_price'] ) ) {
update_post_meta( $product_id, 'main_reward', number_format( floatval( $_POST['_regular_price'] ), '2' ) );
}
if ( isset( $_POST['_sale_price'] ) ) {
update_post_meta( $product_id, 'sub_reward', number_format( floatval( $_POST['_sale_price'] ), '2' ) );
}
}
i am working on save only on sticky post .
added this code as plugin .
add_action( 'draft_to_publish', 'only_one_sticky' );
add_action( 'future_to_publish', 'only_one_sticky' );
add_action( 'new_to_publish', 'only_one_sticky' );
add_action( 'pending_to_publish', 'only_one_sticky' );
add_action( 'publish_to_publish', 'only_one_sticky' );
function only_one_sticky( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! wp_is_post_revision( $post_id ) ) {
$post_id = $post_id->ID;
}
$sticky = ( isset( $_POST['sticky'] ) && $_POST['sticky'] == 'sticky' ) || is_sticky( $post_id );
if( $sticky ) {
$sticky_posts = array();
$sticky_posts_list = get_option( 'sticky_posts', array() );
// The Post IDs are stored in the options table as a single list, so we need to construct a new list with the future posts, plus the newly-published sticky post.
$new_sticky_posts_list = array();
foreach ($sticky_posts_list as $sticky_post) {
$postStatus = get_post_status ( $sticky_post );
if ( get_post_status ( $sticky_post ) != 'publish' || $sticky_post == $post_id ) {
array_push( $new_sticky_posts_list, $sticky_post );
}
}
update_option( 'sticky_posts', $new_sticky_posts_list );
}
}
i deactivate this plugin .
i notice .
1) created a new post - check stick to front
2) again update same post - stick to front unchecked (removed also from db options sticky_posts )
this happen on every post .
im also using custom theme and wpml plugin
Been banging my head against the wall trying to figure out the solution to my current issue. Simply put, I've created a 'page-home.php' template page, can get a meta box to call on the page template, but trying to update the page with data in the meta box makes the data disappear.
Code below:
function page_add_meta_boxes( $post ) {
global $post;
if(!empty($post))
// get the page template post meta
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if the current page uses our specific template, then output our custom metabox
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
// looks for page-home.php file to add our meta box
if($pageTemplate == 'page-home.php' )
{
add_meta_box(
'page-custom-metabox', // $id
'Special Post Meta', // $title
'page_template_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
add_action( 'add_meta_boxes_page', 'page_add_meta_boxes' );
function page_template_metabox() {
wp_nonce_field( basename( __FILE__ ), 'page_meta_box_nonce' );
$some_string = get_post_meta( $post->ID, '_some_string', true );
?>
<input type="text" name="some-string" value="<?php echo $some_string; ?>" />
<?php
}
function page_save_custom_post_meta() {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action( 'publish_page', 'page_save_custom_post_meta' );
add_action( 'draft_page', 'page_save_custom_post_meta' );
add_action( 'future_page', 'page_save_custom_post_meta' );
Any suggestions are appreciated.
Howdy anyone who runs into this in the future. I was able to get it to work with the code below, someone might be able to follow up with a better answer as to why it works. My thinking is that updating my code to 'save_post_page' instead of just 'save_post' makes the difference. (Note I only changed some info due to trying to test it in my theme):
// Add meta box
function frontpage_meta_boxes( $post ){
global $post;
if(!empty($post))
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-home.php' )
{
add_meta_box( 'frontpage_meta_box', __( 'Features' ), 'frontpage_meta_box', 'page', 'advanced', 'high' );
}
}
}
add_action( 'add_meta_boxes_page', 'frontpage_meta_boxes' );
// builds our meta box
function frontpage_meta_box( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'frontpage_meta_box_nonce' );
// retrieve the _manuf_url current value
$manufacturer_url = get_post_meta( $post->ID, '_manuf_url', true );
?>
<h3><?php _e( 'Manufacturer URL' ); ?></h3>
<p>
<input type="text" name="manufacturer-url" value="<?php echo $manufacturer_url; ?>" />
</p>
<?php
}
// saves our data
function frontpage_save_meta_box_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['frontpage_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['frontpage_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// manufacturer url string
if ( isset( $_REQUEST['manufacturer-url'] ) ) {
update_post_meta( $post_id, '_manuf_url', sanitize_text_field( $_POST['manufacturer-url'] ) );
}
// store custom fields values
}
add_action( 'save_post_page', 'frontpage_save_meta_box_data' );
Try with below code :
function page_save_custom_post_meta($postid) {
if ( !isset( $_POST['page_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['page_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// check the user's permissions.
if ( ! current_user_can( 'edit_page', $post_id ) ){
return;
}
// save our string
if ( isset( $_REQUEST['some-string'] ) ) {
update_post_meta( $post_id, '_some_string', sanitize_text_field( $_POST['some-string'] ) );
}
}
add_action('save_post ','page_save_custom_post_meta');
I am trying to add the ability to update a meta value through Wordpress's bulk post edit menu. I have it working on a single post, but I can't figure out why it's not working for bulk.
// Add our text to the quick & bulk edit box
add_action('quick_edit_custom_box', 'on_quick_edit_custom_box', 10, 2);
add_action('bulk_edit_custom_box', 'on_quick_edit_custom_box', 10, 2);
function on_quick_edit_custom_box($column_name, $post_type) {
global $post;
$postMeta = get_post_meta( $post->ID, 'wpsl_locatorID', true );
if ('wpsl_locatorID' == $column_name && get_post_type() == 'location' ) {
echo "<fieldset class=\"inline-edit-col-right\" style=\"margin-top: 0;\">";
echo "<div class=\"inline-edit-col\">";
echo "<div class=\"inline-edit-group\">";
echo "<label class=\"alignleft\">";
echo "<span class=\"title\" for=\"wpsl_locatorID\">Movie ID</span>";
echo "<input type=\"text\" name=\"wpsl_locatorID\" id=\"wpsl_locatorID\" value=\"{$postMeta}\" />";
echo "</label>";
echo "</div>";
echo "</fieldset>";
};
}
// Update the post meta
add_action( 'save_post', 'locationMetaUpdate', 10, 2 );
function locationMetaUpdate( $post_id ) {
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
// if un-commented than the value won't update at all. I'm guessing b/c of WP's ajax function vs. reloading the page.
//if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if ( ! current_user_can( 'edit_posts' ) ) return;
// Make sure that it is set.
if ( ! isset( $_POST['wpsl_locatorID'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['wpsl_locatorID'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'wpsl_locatorID', $my_data );
};
Like I said above, this works for a single quick edit but not on bulk edit. I've tried to find a solution, but I'm sure it's something simple I'm missing.
So honestly I'm not sure why it's now working; but all I did was condense the action like on WP's save_post codex page.
// Update the post meta
add_action( 'save_post', 'locationMetaUpdate', 10, 3 );
function locationMetaUpdate( $post_id, $post, $update ) {
//print_r($_POST); die();
// if our current user can't edit this post, bail
if ( ! current_user_can( 'edit_posts' ) ) return;
// Make sure that it is set.
if ( isset( $_REQUEST['wpsl_locatorID'] ) ) {
update_post_meta( $post_id, 'wpsl_locatorID', sanitize_text_field( $_REQUEST['wpsl_locatorID'] ) );
}
}
I am using the visual composer for the WordPress posts and pages actually for over all. But I want to make some custom meta boxes under the screen of the post editor. Actually already I have made the fields. But now I want to make those fields available in the visual composer. Actually I want to add those fields in the visual editor. How can I do that? Please help me with your valuable knowledge.
Here is my code of the meta boxes
<?php
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
I see that you are echoing your fields as inputs. You need to use the wp_editor() function instead. It will take care of the wysiwyg (visual editor) field creation for you.