1

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
  • 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. 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. Commented Dec 23, 2014 at 4:42

2 Answers 2

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.

1
  • You're right, but please note you've posted this as a separate answer rather than a comment on Rick's answer. Commented Feb 1, 2022 at 10:55
0

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/

1
  • 1
    you can also use ob_start() and ob_get_clean() in order to return your string, instead of using echo Commented Apr 14, 2021 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.