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?

  • 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 Dec 23 '14 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 Dec 23 '14 at 4:42

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';

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

See https://codex.wordpress.org/Function_Reference/add_shortcode

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.