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
Related
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' );
}
}
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 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.
I'm struggling to fix this myself or find answers so here I am. A tag exclusion function I've stitched together is also preventing custom posts with this tag from showing on its corresponding posts page in admin. How can I tweak the function to allow those custom posts to show up?
function exclude_post_by_tag( $query ) {
$excluded_tag_ids = array(47);
if ( $query->is_main_query() ) {
if ( ( $query->is_home() || $query->is_category() ||
$query->is_archive() || $query->is_feed() || $query->is_single() &&
!has_post_format( 'image' ) ) || ( !is_admin() &&
!$query->is_search() ) ) {
$query->set('tag__not_in', $excluded_tag_ids);
} else if ( $query->is_single() ) {
if ( ( $query->query_vars['p'] ) ) {
$page= $query->query_vars['p'];
} else if ( isset( $query->query_vars['name'] ) ) {
$page_slug = $query->query_vars['name'];
$post_type = 'post';
global $wpdb;
$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
}
if ( $page ) {
$post_tags = wp_get_post_tags( $page );
foreach ($excluded_tag_ids as $tag_id ) {
if ( in_array( $tag_id, $post_tags ) ) {
$query->set( 'p', -$tag_id );
break;
}
}
}
}
}
}
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
The custom post type I'm using is called data and the posts use a 'standard' post format.
Thanks so much for reading, I hope someone can help.
At the begin of your function, abort execution by checking if in admin context:
function exclude_post_by_tag( $query ) {
if( is_admin() )
return;
// rest of the code here
}
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
I've just found the answer. I just needed to change:
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
to:
if( !is_admin() ){
add_action( 'pre_get_posts', 'exclude_post_by_tag' );
}
to prevent it affecting the admin pages. Whew!
I want to change the slug of a post with a custom field.
In example, if the custom field is "keyword" my post link will become: mysite.com/keyword.
I wrote this script in fonction.php:
function change_default_slug($id) {
// get part number
$partno = get_post_meta( $id, 'partno', true );
$post_to_update = get_post( $id );
// prevent empty slug, running at every post_type and infinite loop
if ( $partno == '' )
return;
$updated_post = array();
$updated_post['ID'] = $id;
$updated_post['post_name'] = $partno;
wp_update_post( $updated_post ); // update newly created post
}
add_action('save_post', 'change_default_slug');
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $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( !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_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
$partno = get_post_meta($post->ID,'my_meta_box_text',true);
echo $partno;
This script return me "Fatal error: Maximum execution time of 30 seconds exceeded". But it seems it work because my slug change. Any idea about this issue?
The 'save_post' action gets called by wp_update_post(), so your change_default_slug() function causes an infinite loop. You need to perform a check within change_default_slug() and bail out if the function has already been called:
function change_default_slug($id) {
static $beentheredonethat = false;
if ($beentheredonethat) return;
$beentheredonethat = true;
//do your stuff and save the post...
}