Get post_type in current_screen
-
Hello,
I want to know if there is a way to get the post_type “tablepress_table” when I’m editing a table ?
I need this so I can prevent a plugin to execute on that screen and also the code must be as generic as possible.
any input ?
-
Hi,
thanks for your post, and sorry for the trouble.
To be honest, I’m not really sure what you mean here, but from what I understand, you might simply have to check the global variable, i.e. use something like
global $post_type; (then use $post_type in your checks)
Regards,
TobiasHello,
Thanks you for this great plugin.
Most of the time I use mqtranslate for the multilingual sites. So I’m adding a feature so the admin can chose the custom post types that don’t require mqtranslate. Or the custom post type that get corrupted by the mqtranslate (the case of tazblepress).
I already fix this problem for several plugins (cform7, cpt-onomies, etc) where I didn’t have a problem to get the post_type but in tablepress I’m unable to retrieve the post_type.
Exemple I’m in :
https://example.com/wp-admin/admin.php?page=tablepress&action=edit&table_id=24In this screen I’m trying to retrieve the post_type
like thisfunction qtrans_disableTranslation(){ global $q_config; echo "<pre>"; var_dump(qtrans_getCurrentPostType()) ; echo "</pre>"; //Tablepress always return null or wrong post_type if( in_array(qtrans_getCurrentPostType(), $q_config['ignored_custompost'] ) ) { define('QT_DISABLED', true); return false; } /**/ return true; } function qtrans_admin_notice_disabled_bypost() { if(!qtrans_disableTranslation()): ?> <div class="update-nag"> <p><?php _e( 'Translation disabled on this post type', 'mqtranslate' ); ?></p> </div> <?php endif; } add_action( 'admin_notices', 'qtrans_admin_notice_disabled_bypost' ); function qtrans_getCurrentPostType() { global $post, $typenow, $current_screen, $post_type; if( $post_type ) return $post_type; $post_id = ( isset($_GET['post']) ) ? $_GET['post'] : ( isset($_POST['post_ID']) ) ? $_POST['post_ID'] : 0; $post = NULL; $post_type_object = NULL; if ( $post_id && $post = get_post($post_id) ) { if ( $post_type_object = get_post_type_object($post->post_type) ) { return $post_type_object->name; } } elseif ( isset($_POST['post_type']) && $post_type_object = get_post_type_object($_POST['post_type']) ) { return $post_type_object->name; } elseif( $typenow ){ return $typenow; } elseif( $current_screen && $current_screen->post_type ){ return $current_screen->post_type; } elseif( isset( $_REQUEST['post_type'] ) ){ return sanitize_key( $_REQUEST['post_type'] ); }elseif (get_post_type($_REQUEST['post'])){ return get_post_type($_REQUEST['post']); } //last chance to get the post_type (this can be wrong) $regex = "/^.*(id)$/i"; $vars = array(); foreach($_GET as $name=>$value) { if(preg_match($regex, $name)) { $vars[$name] = $value; if ( $post = get_post($value) ) { if ( $post_type_object = get_post_type_object($post->post_type) ) { return $post_type_object->name; } } } } return "undefined_posttype"; }
At the end of qtrans_getCurrentPostType I’m doing a loop to see if we have in the query something like xxxx_id and try to guest the posttype by that ID… but in tablepress table_id is not the same as the post’s id.
So for example in my case table_id=24 will uses the wp_posts with the ID 586MMMMhhhh…. hope I was clear.
By the way I tried several actions in the add_action (https://codex.www.ads-software.com/Plugin_API/Action_Reference) without successHi,
ok, I think I see what you are trying to do here. The reason why the
tablepress_table
post type is not set here is that the “Edit” screen for tables is not directly an “Edit” screen for the posts with that post type, but a custom implementation.
Can you maybe check some of the values thatget_current_screen()
returns, for whether they containtablepress
(additionally to that post type check)?
I haven’t tested mqTranslate personally, but from my support experience, the problem usually is that it seems to be filtering calls towp_update_post()
and then messes with the JSON code there. This should however get better with the mime type support that is being added (see https://www.ads-software.com/support/topic/disable-auto-multilanguage-taging-in-certai-custom-post-type?replies=12 )Regards,
TobiasWell I can do that but I really don’t like to work with exceptions when working with a framework.
The condition to get the post_type of tablepress is kind of dirty and must be configured by hand :
function qtrans_customTest($test_parent_screen,$test_action, $post_type){ $screen = get_current_screen(); $current_parent_screen = $screen->parent_base; if(!$current_action = $screen->action){ $current_action = $screen->base; $test_action = $current_parent_screen.'_'.$test_action ; } return ($current_parent_screen == $test_parent_screen && $current_action == $test_action) ? $post_type : false; }
And the condition :
if( defined( 'TABLEPRESS_ABSPATH' ) && $post_type = qtrans_customTest('tablepress', 'edit', 'tablepress_table') ){ return $post_type ; }
That would be great if tablepress return WP_Screen with all the variables filled in the object.
Well this is my 2 cents ??thanks for the support
Hi,
I totally understand that having exceptions in a framework is not really nice.
I don’t really see how I could make this easier for you though. The WP_Screen object is filled by WordPress, and TablePress uses the regularadd_menu_page
API to add an admin screen.
If you have suggestions about what I should change in the TablePress code to make your life easier, I’m very open to hearing that!Now, why are you actually trying to change the admin screen itself? Shouldn’t it be enough to make checks for the post type in the filter hook for
wp_update_post()
that mqTranslate likely uses?Regards,
Tobias
- The topic ‘Get post_type in current_screen’ is closed to new replies.