php - How to use $_GET function Wordpress backend

admin2025-01-07  7

I use Plugin Boilerplate for my project. I searched the internet and read all the questions but I couldn’t find what I need. I also read the WordPress Codex. I think I couldn’t get the idea.

I have two columns on my page. Col1 and Col2

Inside col1: wp_list_table
Inside col2: empty

Page address: admin.php?page=company#home

When I clicked the Add New button on wp_list_table, the address bar shows admin.php?page=company#home&action=new

I want to get the action value from the url. I tried the code below:

function addnew_query_vars($vars) {
 $vars[] = 'action';
 return $vars;
}
add_filter('query_vars', 'add_query_vars_filter');

echo $value = get_query_var('action'); // Nothing happens

How I can get action value? If I can get the value, I’ll show a form inside the col2 or using a switch statement for other situations.


UPDATE 1.1.0

Using get_current_screen

require_once(ABSPATH . 'wp-admin/includes/screen.php');                     
$screen = get_current_screen();
echo $screen->action; // Null

I'm using query monitor plugin. I looked Admin Screen status get_current_screen() action is empty. So I need to go back.

In the my WP_List_Table header code, my Add New button code is like this:

<a href="<?php echo admin_url( 'admin.php?page=company#home&action=new' );?>">
php _e( 'Add New', 'ironhead' )
</a>

I think this code block doesn't post the action. There is set_current_screen() command. But I don't solve how to set Admin Screen action attribute. IF I can use it, I can use get_current_screen(). Anyone help me?

ALL CODE

TAB PANE

<ul class="nav nav-tabs nav-pills tab-pane" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Company</a>
</li>
<li class="nav-item">
<a class="nav-link" id="facility-tab" data-toggle="tab" href="#facility"  role="tab" aria-controls="facility" aria-selected="false">Facility</a>
</li>
...
</ul>

TAB CONTENT

<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="row">
<div class="col">
<?php
function wp_get_all_company( $args = array() ) {
global $wpdb;

$defaults = array(
'number'     => 20,
'offset'     => 0,
'orderby'    => 'ID',
'order'      => 'ASC',
);

$args      = wp_parse_args( $args, $defaults );
$cache_key = 'company-all';
$items     = wp_cache_get( $cache_key, 'ironhead' );

if ( false === $items ) {
$items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'ih_company ORDER BY ' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );

wp_cache_set( $cache_key, $items, 'ironhead' );
}

return $items;
}

function wp_get_company_count() {
global $wpdb;

return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'ih_company' );
}

function wp_get_firma( $id = 0 ) {
global $wpdb;

return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'ih_company WHERE ID = %d', $id ) );
} ?>

<h4><?php _e( 'Company List', 'ironhead' ); ?> <a href="<?php echo admin_url( 'admin.php?page=company#home&action=new' ); ?>" class="add-new-h2"><?php _e( 'Add New', 'ironhead' ); ?></a></h4>

<form method="post">
<input type="hidden" name="page" value="ttest_list_table">

<?php
$list_table = new Company_List_Table();
$list_table->prepare_items();
$list_table->search_box( 'search', 'search_id' );
$list_table->display();
?>
</form>
</div>
<div class="col">

// action=new (or action=something) if or switch statement will be here.

</div>
</div>

Thanks in advance

I use Plugin Boilerplate for my project. I searched the internet and read all the questions but I couldn’t find what I need. I also read the WordPress Codex. I think I couldn’t get the idea.

I have two columns on my page. Col1 and Col2

Inside col1: wp_list_table
Inside col2: empty

Page address: admin.php?page=company#home

When I clicked the Add New button on wp_list_table, the address bar shows admin.php?page=company#home&action=new

I want to get the action value from the url. I tried the code below:

function addnew_query_vars($vars) {
 $vars[] = 'action';
 return $vars;
}
add_filter('query_vars', 'add_query_vars_filter');

echo $value = get_query_var('action'); // Nothing happens

How I can get action value? If I can get the value, I’ll show a form inside the col2 or using a switch statement for other situations.


UPDATE 1.1.0

Using get_current_screen

require_once(ABSPATH . 'wp-admin/includes/screen.php');                     
$screen = get_current_screen();
echo $screen->action; // Null

I'm using query monitor plugin. I looked Admin Screen status get_current_screen() action is empty. So I need to go back.

In the my WP_List_Table header code, my Add New button code is like this:

<a href="<?php echo admin_url( 'admin.php?page=company#home&action=new' );?>">
php _e( 'Add New', 'ironhead' )
</a>

I think this code block doesn't post the action. There is set_current_screen() command. But I don't solve how to set Admin Screen action attribute. IF I can use it, I can use get_current_screen(). Anyone help me?

ALL CODE

TAB PANE

<ul class="nav nav-tabs nav-pills tab-pane" id="myTab" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Company</a>
</li>
<li class="nav-item">
<a class="nav-link" id="facility-tab" data-toggle="tab" href="#facility"  role="tab" aria-controls="facility" aria-selected="false">Facility</a>
</li>
...
</ul>

TAB CONTENT

<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<div class="row">
<div class="col">
<?php
function wp_get_all_company( $args = array() ) {
global $wpdb;

$defaults = array(
'number'     => 20,
'offset'     => 0,
'orderby'    => 'ID',
'order'      => 'ASC',
);

$args      = wp_parse_args( $args, $defaults );
$cache_key = 'company-all';
$items     = wp_cache_get( $cache_key, 'ironhead' );

if ( false === $items ) {
$items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'ih_company ORDER BY ' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );

wp_cache_set( $cache_key, $items, 'ironhead' );
}

return $items;
}

function wp_get_company_count() {
global $wpdb;

return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'ih_company' );
}

function wp_get_firma( $id = 0 ) {
global $wpdb;

return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'ih_company WHERE ID = %d', $id ) );
} ?>

<h4><?php _e( 'Company List', 'ironhead' ); ?> <a href="<?php echo admin_url( 'admin.php?page=company#home&action=new' ); ?>" class="add-new-h2"><?php _e( 'Add New', 'ironhead' ); ?></a></h4>

<form method="post">
<input type="hidden" name="page" value="ttest_list_table">

<?php
$list_table = new Company_List_Table();
$list_table->prepare_items();
$list_table->search_box( 'search', 'search_id' );
$list_table->display();
?>
</form>
</div>
<div class="col">

// action=new (or action=something) if or switch statement will be here.

</div>
</div>

Thanks in advance

Share Improve this question edited Mar 24, 2019 at 19:08 Pecado asked Mar 18, 2019 at 19:48 PecadoPecado 93 bronze badges 10
  • Where is the #home coming from? Are you adding that to the page url? IIRC, Anything that comes after the # is not sent to the server. – czerspalace Commented Mar 18, 2019 at 20:35
  • Hi @czerspalace My active tab name is "home". col1 and col2 is inside it. – Pecado Commented Mar 18, 2019 at 21:03
  • Are you adding the active tab name yourself? You would need to change the Add New to admin.php?action=new&page=company#home – czerspalace Commented Mar 18, 2019 at 21:08
  • I wanna do everything in one page. So, I used bootstrap tabs. – Pecado Commented Mar 18, 2019 at 21:27
  • The # is causing the action to not go to the server. So the #home needs to be at the end of the query string, so like admin.php?action=new&page=company#home or admin.php?page=company&action=new#home. Can you post code of the how the "Add New" button is generated? – czerspalace Commented Mar 19, 2019 at 17:16
 |  Show 5 more comments

3 Answers 3

Reset to default 0

Working in the admin is quite a bit different from the front-end of a WordPress site. Most of the documentation you'll find online deals solely with the front-end. That can be frustrating when looking for something that should be simple and straightforward.

In this case, I think you would use get_current_screen(). This returns the screen's ID, action, base, and other features of the admin page.

From the codex on get_current_screen():

This function returns an object that includes the screen’s ID, base, post type, and taxonomy, among other data points

This doesn't work everywhere in the admin, so make sure you read the restrictions.

I don't know it's true or false, there is a solution with form element and old PHP knowledge :)

FORM

<form role="form" method="post" action="admin.php?page=company#home">
<h4><?php _e( 'Firma Listesi', 'ironhead' ); ?>
<input id="action" name="action" hidden="" type="text" value="new">
<button id="new" name="action_new" class="add-new-h2"><?php _e( 'Add New', 'ironhead' ); ?></button></h4>
</form>

POST

echo @$_POST['action'];

Is there any true method?

@Pecado yes, $_POST['action'] will work. I have created a page in admin section and I've used form to submit and retrieving form field on $_POST

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258410a510.html

最新回复(0)