最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Wordpress Shortcode callback function with a plugin

programmeradmin0浏览0评论

I am working on a plugin and the plugin currently works great on the admin section but I'm trying to extend the functionality to the front end and using a shortcode to use the same functions. I have a form that gets displayed currently with the following code:

function display_sheet_form($f=array())
{

    $count = (isset($f['task_title'])) ? count($f['task_title']) : 3;
    if ($count < 3) $count = 3;

    echo '<form name="add_sheet" id="dls-sus-modify-sheet" method="post" action="">';
    //some additional code here
    echo '<input type="hidden" name="mode" value="submitted" />';
    echo '<input type="submit" name="Submit" class="button-primary" value="'.esc_attr('Save').'" />';
}

This form doesn't have an action but the from the submenu it has a function callback that it knows to post this to within the same page. The code for the submenu is:

add_submenu_page($this->admin_settings_slug.'_sheets', 'Add New Sheet', 'Add New', 'manage_signup_sheets', $this->admin_settings_slug.'_modify_sheet', array(&$this, 'modify_sheet_page'));

There you can see that the callback function is the modify_sheet_page. Here is the code for this function:

function modify_sheet_page()
{
    if (!current_user_can('manage_options') && !current_user_can('manage_signup_sheets'))  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    echo "in the modify sheet page";
    // Set mode vars
    $edit = (empty($_GET['sheet_id'])) ? false : true;
    $add = ($edit) ? false : true;
    $submitted = (isset($_POST['mode']) && $_POST['mode'] == 'submitted');
    $err = 0;

    // Process form if submitted
    if($submitted) {
    //process the form code
    }
}

I created my shortcode like this:

add_shortcode('create_sign_up_sheet', array(&$this->admin, 'display_sheet_form'));

Currently it's saying the action is the page that the template is being displayed on since it can't use the callback of the submenu page. How can I post to the same modify_sheet_page() function using this shortcode?

I am working on a plugin and the plugin currently works great on the admin section but I'm trying to extend the functionality to the front end and using a shortcode to use the same functions. I have a form that gets displayed currently with the following code:

function display_sheet_form($f=array())
{

    $count = (isset($f['task_title'])) ? count($f['task_title']) : 3;
    if ($count < 3) $count = 3;

    echo '<form name="add_sheet" id="dls-sus-modify-sheet" method="post" action="">';
    //some additional code here
    echo '<input type="hidden" name="mode" value="submitted" />';
    echo '<input type="submit" name="Submit" class="button-primary" value="'.esc_attr('Save').'" />';
}

This form doesn't have an action but the from the submenu it has a function callback that it knows to post this to within the same page. The code for the submenu is:

add_submenu_page($this->admin_settings_slug.'_sheets', 'Add New Sheet', 'Add New', 'manage_signup_sheets', $this->admin_settings_slug.'_modify_sheet', array(&$this, 'modify_sheet_page'));

There you can see that the callback function is the modify_sheet_page. Here is the code for this function:

function modify_sheet_page()
{
    if (!current_user_can('manage_options') && !current_user_can('manage_signup_sheets'))  {
        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
    }
    echo "in the modify sheet page";
    // Set mode vars
    $edit = (empty($_GET['sheet_id'])) ? false : true;
    $add = ($edit) ? false : true;
    $submitted = (isset($_POST['mode']) && $_POST['mode'] == 'submitted');
    $err = 0;

    // Process form if submitted
    if($submitted) {
    //process the form code
    }
}

I created my shortcode like this:

add_shortcode('create_sign_up_sheet', array(&$this->admin, 'display_sheet_form'));

Currently it's saying the action is the page that the template is being displayed on since it can't use the callback of the submenu page. How can I post to the same modify_sheet_page() function using this shortcode?

Share Improve this question edited Dec 23, 2014 at 4:40 user1048676 asked Dec 23, 2014 at 0:17 user1048676user1048676 4373 gold badges9 silver badges25 bronze badges 2
  • 2 The code you posted should not work in its current state. You should (depending on if you have errors turned on) either get a White Screen Of Death or just an error message. Your brackets aren't closed, your quotes are misplaced or closed too early (or missing), etc. Please fix that and post the code you are really using. You may also want to read about forms and the action attribute and what it actually does. – kaiser Commented Dec 23, 2014 at 2:20
  • @kaiser I made the changes to the code. There is a lot of form processing and form elements in between the code blocks so was just trying to simplify it to make this readable. I'm just trying to really figure out how to call the correct functions without duplicating them. – user1048676 Commented Dec 23, 2014 at 4:42
Add a comment  | 

2 Answers 2

Reset to default 1

Rick's answer is incorrect, as the documentation for add_shortcode specifies that "the function called by the shortcode should never produce an output of any kind". Instead, you should return the text that you want the shortcode to output. So, try having display_sheet_form return that text instead of echoing it.

The add_shortcode() function's second parameter should be a function that outputs text/content.

In your case, you should create a function that outputs text (or the contents of an array, but not the arrray) and use that in the add_shortcode().

add_shortcode('myshortcode', 'myshortcodefunction');
function myshortcodefunction() {
  // set some values
  echo 'some values';
return;
}

Then the shortcode [myshortcode] will translate to 'some values'.

See https://developer.wordpress.org/reference/functions/add_shortcode/

发布评论

评论列表(0)

  1. 暂无评论