Wordpress: How to check if current post is saved/updated - wordpress

How can I check in backend if the current post is saved/updated or not.
I need to show an metabox after the post is saved/updated.
Literally after the "Update" Button is clicked.
something like this small sketch:
function add_aftersave_metabox() {
///how to do this:
if ( $post == ?? is saved?? )
remove_meta_box('you-have-to-save', 'post', 'normal');
add_meta_box( 'post-is-saved', 'post', 'normal'); }
}
add_action('admin_head', 'add_aftersave_metabox');
Thanks for any help!

Just to make sure I understand, you don't just want to look at the blog section of your page to see if the post is posted, but you want to check it otherwise?
I am just curious as to why you may want to do so with Wordpress.

Related

Wordpress manipulate comment_reply_link() add #nav-comform to the link

I am working on a theme for a friend but get stuck ...
The comments and the comment form are inside a jquery tab.
To toggle the tab on a klick of the reply link i have to add #nav-comform to the link.
Example:
http://localhost/?p=109&replytocom=10#respond#nav-comform
I know i have to work with a filter in the functions.php but i have never done it before so i am a little lost and everything i try fail ...
I know it should be something like this filter example to add rel="nofollow" to the reply link:
function add_nofollow_to_reply_link( $link ) {
return str_replace( '")\'>', '")\' rel=\'nofollow\'>', $link );
}
add_filter( 'comment_reply_link', 'add_nofollow_to_reply_link' );
Maybe some one can lead me a way ?
Thank you very much !!
Try the following
function add_link_hash($args){
$args['respond_id'] = 'nav-comform';
return $args;
}
add_filter('comment_reply_link_args', 'add_link_hash', 10);

Wordpress custom meta box for one page only

everyone!
I believe similar question have been asked already, but I had not found working solution.
The problem: I need a metabox for one exact page only.
The code I use to add a metabox is pretty simple:
function custom_meta_boxes(){
add_meta_box('custom_meta_1', 'About Us Main field','custom_meta_boxes_render',
'page','normal','high','');
}
If you look on the 4th parameter - 'page', this puts metabox on every page, so that I see it and can use it for every page I created when editing it. And I need to see it on about us page only (for example).
I`ve seen in some tutorials that instead of 'page' you can use an ID or a slug of page, but that did not work for me.
Therefore, I really need your help/advice on that.
Thanks a lot in advance.
Try to do it like this:
<?php
function custom_meta_boxes(){
global $post;
$postSlug = isset($post->slug) ? $post->slug : '';
if ($post->slug === 'about-us') {
add_meta_box(
'custom_meta_1',
'About Us Main field',
'custom_meta_boxes_render',
'page',
'normal',
'high',
''
);
}
}
And change about-us slug for that you want to add meta box.

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

How do you remove or change the functionality of the Publish button on a custom WordPress post?

I have a custom post type and need keep the post status from getting set to 'Published' when you click the Publish button. Instead, it should work like the Save Draft button. So I either need to figure out how to just remove the Publish button so the user's can only click Save Draft our preferably, update the Publish button functionality so it doesn't set the post to publish.
You can use wordpress action hooks to modify default behaviors.
http://codex.wordpress.org/Function_Reference/add_action
In your case, you want to use the 'publish_post' hook.
So you can do
function dont_publish( $post_ID )
{
if(get_post_type($post_ID) == 'your_custom_type'){
exit;
}
}
//the dont_publish function will be called after the publish button is clicked
add_action( 'publish_post', 'dont_publish' );
The way it is above, nothing will happen at all if the publish button is clicked, but you can play around with the dont_publish function to get the results you want.
#PhoenixWing156 was close but one little change so the the other post types get updated as usual.
function dont_publish( $data , $postarr ) {
if($data['post_type'] == 'custom_post_type') {
$data['post_status'] = 'draft';
}
return $data;
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
The wp_insert_post_data hook is called before information about a post is saved to the database.
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
You can try:
function dont_publish( $data , $postarr )
{
if($data['post_type'] == 'custom_post_type'){
$data['post_status'] = 'draft';
return $data;
}
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
WordPress provides the remove_meta_box() function exactly for this purpose.Just add this below code:-
add_action( 'admin_menu', function () {
remove_meta_box( 'submitdiv', 'Your_custom_post_type', 'side' );
} );
You could also disable the default saving metabox and add you own.
This is not documented well in the developer docs of wordpress.
To do this you have to hook into the "add_meta_boxes"-hook and in the hooked function yo have to call remove_meta_box('submitdiv','your-cpt','side');
The code should be something like this:
function your_cpt_metaboxes(){
remove_meta_box('submitdiv','your-cpt','side');
...
}
add_action('add_meta_boxes','function your_cpt_metaboxes');
your-cpt has to be changed to the name of your cpt of course.
I was also searching for this handy snippet and found it in the plugin Awesome Support.
The original saving metabox code can be found in /wp-admin/includes/metaboxes.php .
Just search for post_submit_meta_box (in WP 5.4 on line 22).

How do you make a Wordpress plugin have "per post" options?

I'm building a Wordpress blog that requires a custom slideshow system with an admin panel on each post admin page (sort of like how Yoast's SEO plugin has options on each page).
How would I make the admin options appear on the post admin page?
Thanks,
Charlie
You are not providing much detail about your project but you probably want to make some meta_boxes and save the data to as custom fields.
Here is a truncated example culled from something I put together for a question at wordpress.stackexchange. The details of some of the functions are not important for your question here but it illustrates the general 'how'. Follow the link for working but sincerely beta code.
// author checkboxes
add_action( 'add_meta_boxes', 'assisting_editor' );
function assisting_editor() {
add_meta_box(
'assisting_editor', // id, used as the html id att
__( 'Editorial Tasks' ), // meta box title
'editor_tasks', // callback function, spits out the content
'post', // post type or page. This adds to posts only
'side', // context, where on the screen
'low' // priority, where should this go in the context
);
}
// this is the callback that creates the meta box content
function editor_tasks( $post ) {
// function details skipped; follow the link
}
// to save the data
add_action( 'save_post', 'save_metadata');
// callback for the save hook ^^
function save_metadata($postid) {
// function details skipped; follow the link
}

Resources