I'm developing a plugin and one of the issues I am running into is that I cannot get the post id within a function assigned to the admin_init hook.
I tried a few different methods; but, they all seem to use the $wp_query.
Below is a simple version the code I am using. I implemented the code like this just now and ran it by viewing the "post edit" page
add_action('admin_init','do_optional_featured_article');
function do_optional_featured_article()
{
global $wp_query;
echo "<pre>";
print_r($wp_query);
echo "</pre>";
die();
}
$wp_query is a mostly empty array, notably, the post member is empty
-- EDIT --
I got some advice over at wordpress.stackexchange and added in this function:
function get_admin_post()
{
if( isset($_GET['post']) )
{
$post_id = absint($_GET['post']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
elseif( isset($_POST['post_ID']) )
{
$post_id = absint($_POST['post_ID']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
else
{
return false;
}
}
I think this answer will help. It states that the earliest action you can hook into, to get the global $post/$posts variables is the wp action. In the action hook reference on the codex, you can see that the wp action executes a bit after admin_init, which is why you can't retrieve any posts there, I think.
So, that should work:
add_action('wp','do_optional_featured_article');
Related
I'm trying to show some calculated information after each comment, which are planned to be displayed only in 'product' post type. I have no problem with making the function work, it is showing what I want, but it also causes fatal errors on my website, for example the Comments section in admin panel. How can I make sure that these information are only shown in post type of 'product' and doesn't cause any errors anywhere else? My attempt is as follows:
function comment_significance_indicator ($content, $comment) {
global $wpdb;
global $current_user;
global $post;
$post_type = get_post_type( $post->ID );
if ($post_type === 'product') {
//some stuff happens here
return $content . $blabla;
else {
return $content;
}
}
add_filter("comment_text","comment_significance_indicator",50,2);
Thanks for any help.
write the code when the post_type == product like below:
if ($post_type === 'product') {
function your_func(){
return ;
}
add_filter("comment_text","comment_significance_indicator",50,2);
}
I am developing a plugin and need the list of post types existing in the WordPress backend, I checked the WP_Query page of WordPress codex but could not find the solution
You can try this code.
<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever. Any time after init is usually
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init()
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
if ( isset( $type->rewrite->slug ) ) {
// you'll probably want to do something else.
echo $type->rewrite->slug;
}
}
}
i think it will work for you
I am getting this error "Uncaught Error: Option 'ajax' is not allowed for Select2 when attached to a element." while updating Product Variation.
Actually there are 2 select2.js files, one from Woocommerce and other from 'WR PageBuilder' plugin. While I am renaming 'WR PageBuilder' select2.js file then its working fine. But that file is required for Editor.
I want to remove that js file only from Product pages.
I did 'wp_deregister_script()' and 'wp_dequeue_script()' but nothing happened.
Here is my code:
add_action('admin_init', 'functon_to_filter_script');
function functon_to_filter_script() {
global $typenow;
// when editing pages, $typenow isn't set until later!
if (empty($typenow)) {
// try to pick it up from the query string
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
}
if( 'product' == $typenow ){
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
}
}
function deregister_my_script() {
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
can anyone give me a solution?
This won't work because you are using the actions wrong.
Look here for the correct usage of action hooks:
Hooks in Wordpress
You are putting the admin_enqueue_scripts action hook inside of the admin_init action hook.
Try taking admin_enqueue_scripts outside of the admin_init hook like this:
global $typenow;
add_action( 'admin_enqueue_scripts', 'deregister_my_script', 100 );
function deregister_my_script() {
if (!empty($_GET['post'])) {
$post = get_post($_GET['post']);
$typenow = $post->post_type;
}
if( 'product' == $typenow ){
wp_dequeue_script('wr-pagebuilder');
wp_deregister_script('wr-pagebuilder');
}
}
In WordPress with the publish_{post_type} (http://codex.wordpress.org/Post_Status_Transitions) you can hook into the loop if a post with custom type is published. But publish_{post_type} is also triggers if a a post is updated.
I am basically looking for a way to check the old and new status, within the publish_post_type hook. Does anyone have a nifty idea as to how to accomplish that?
I am doing this:
function doStuff( $post_ID ) {
// do stuff
return $post_ID;
}
add_action( 'publish_ttplaned', 'doStuff' );
So basically I need to check the old and new status of the post within the fuction doStuff().
In case someone else is looking for the aswer. I ended up using Williams solution.
add_action( 'draft_to_publish', 'doStuff', 10, 1 );
add_action( 'pending_to_publish', 'doStuff', 10, 1 );
function doStuff( $post ) {
if ( get_post_type( $post ) == "my_custom_post_type" ){
// do stuff
}
}
This works since a new post starts out as a draft even if you publish it right away.
As #rnevius says, there's no hook in WordPress core called publish_post_type.
There is also publish_post - which I've just tested it to make sure it only runs when the post is first published, not when it's subsequently edited.
So you can do something like:
function my_function($post_ID, $post) {
if ("foo" == $post->post_type) {
// do something
}
}
add_action( 'publish_post', 'my_function', 10, 2 );
There is also the wp_transition_post_status(), which calls several actions:
transition_post_status with $new_status, $old_status, $post
or, if you want to do it the other way around:
{$old_status}_to_{$new_status} and passes a WP_Post object as the only parameter.
In a plugin, via the add_action() routine, I try to run a check. But getting the post ID as below doesn't work, but why? What's the correct way of getting the post ID and a related cat id?
add_action( 'wp', 'check_url', 10, 1 );
function check_url($wp){
if( is_single() ){
$cat_id = wp_get_post_categories( $post->ID );
}
}
Add global to your function first before using $post to make it visible inside your function (this is why we love PHP):
function check_url($wp){
global $post;
...
}