Skip to main content
Better answer
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

Is there anything special for dealing with $_POST in this case?

Shortcodes are case-sensitive Maybe, soif you can elaborate more on what you mean by "special"?

But as with other PHP arrays, you should always check if the POST variable is actually set before attempting to use it:

  • Use [Edit_field Field="Email_1"]

  • And not [Edit_Field Field="Email_1"]

// Example for the Edit_Email_1 input:
if ( isset( $_POST['Edit_Email_1'] ) ) {
    return $_POST['Edit_Email_1'];
}

BecauseSecondly, you definedshould also escape the shortcodevalue just as with any user-supplied or untrusted data, e.g. using add_shortcodeesc_html('Edit_field', 'Edit_field'); — note the firstor Edit_fieldabsint(), where if the input should be a (non-negative) number, or fesc_attr() if the value is to be displayed in lowercasea form field like <input>.

Secondly, WordPress converts the shortcode attributes to lowercase, soSo for the above reason, you might want to add a $Fieldcontext inattribute to your caseshortcode which will determine whether the value should be undefined and thereforeescaped, sanitized or returned as-is (i.e. raw/unchanged).

On the front end I am anticipating to use:

[Edit_Field Field="Email_1"]

Yes, you can do so, but you should use $field instead.know that:

  1. Shortcodes are case-sensitive, so you should:

    • Use [Edit_field Field="Email_1"]

    • And not [Edit_Field Field="Email_1"]


    Because you defined the shortcode as add_shortcode('Edit_field', 'Edit_field'); — note the first Edit_field, where the f is in lowercase.

  2. WordPress converts the shortcode attributes (i.e. the attribute name) to lowercase, so the $Field in your Edit_field() function will be empty and you should use $field instead along with 'field' => '' in your shortcode_atts() array.

However, please just avoid using extract()avoid using extract() and use the $atts instead to access the shortcode attributes:

Note: I've applied the context attribute in this function.

function Edit_field( $atts ) {
    // Don't use extract().
    $atts = shortcode_atts( array(
        'field'   => '',
        'context' => 'view',
    ), $atts );

    if ( $atts['field'] == 'Email_1' && isset( $_POST['Edit_Email_1'] ) ) {
        return $_POST['Edit_Email_1']( ?'edit' === $atts['context'] ) ? 
 'NA';           esc_attr( $_POST['Edit_Email_1'] ) :
            esc_html( $_POST['Edit_Email_1'] );
    }

    if ( $atts['field'] == 'Client_Description' && isset( $_POST['Edit_Client_Description'] ) ) {
        return $_POST['Edit_Client_Description']( ??'edit' 'NA';=== $atts['context'] ) ?
    }
}

Additionally, always check if the POST variable is actually set:

// Example for the Edit_Email_1 variable   esc_attr( $_POST['Edit_Client_Description'] ) :
return isset           esc_html( $_POST['Edit_Email_1']$_POST['Edit_Client_Description'] );
 ? $_POST['Edit_Email_1'] : 'NA';}

    // useShortcodes this
returnshould $_POST['Edit_Email_1']always ??return 'NA';something.
 // or like Ireturn used''; in// the.. aboveeven function,if usingit's PHPan 7empty formatstring.
}

And because the attribute names are lowercased, then just use lowercase in the shortcode like so:

<p>[Edit_field field="Email_1"]</p>
<p>[Edit_field field="Client_Description"]</p>

<input value='[Edit_field field="Email_1" context="edit"]'>
<input value='[Edit_field field="Client_Description" context="edit"]'>

Shortcodes are case-sensitive, so you should:

  • Use [Edit_field Field="Email_1"]

  • And not [Edit_Field Field="Email_1"]

Because you defined the shortcode as add_shortcode('Edit_field', 'Edit_field'); — note the first Edit_field, where the f is in lowercase.

Secondly, WordPress converts the shortcode attributes to lowercase, so the $Field in your case will be undefined and therefore, you should use $field instead.

However, please just avoid using extract() and use the $atts instead to access the shortcode attributes:

function Edit_field( $atts ) {
    $atts = shortcode_atts( array(
        'field' => '',
    ), $atts );

    if ( $atts['field'] == 'Email_1' ) {
        return $_POST['Edit_Email_1'] ?? 'NA';
    }

    if ( $atts['field'] == 'Client_Description' ) {
        return $_POST['Edit_Client_Description'] ?? 'NA';
    }
}

Additionally, always check if the POST variable is actually set:

// Example for the Edit_Email_1 variable:
return isset( $_POST['Edit_Email_1'] ) ? $_POST['Edit_Email_1'] : 'NA'; // use this
return $_POST['Edit_Email_1'] ?? 'NA'; // or like I used in the above function, using PHP 7 format

Is there anything special for dealing with $_POST in this case?

Maybe, if you can elaborate more on what you mean by "special"?

But as with other PHP arrays, you should always check if the POST variable is actually set before attempting to use it:

// Example for the Edit_Email_1 input:
if ( isset( $_POST['Edit_Email_1'] ) ) {
    return $_POST['Edit_Email_1'];
}

Secondly, you should also escape the value just as with any user-supplied or untrusted data, e.g. using esc_html() or absint() if the input should be a (non-negative) number, or esc_attr() if the value is to be displayed in a form field like <input>.

So for the above reason, you might want to add a context attribute to your shortcode which will determine whether the value should be escaped, sanitized or returned as-is (i.e. raw/unchanged).

On the front end I am anticipating to use:

[Edit_Field Field="Email_1"]

Yes, you can do so, but you should know that:

  1. Shortcodes are case-sensitive, so you should:

    • Use [Edit_field Field="Email_1"]

    • And not [Edit_Field Field="Email_1"]


    Because you defined the shortcode as add_shortcode('Edit_field', 'Edit_field'); — note the first Edit_field, where the f is in lowercase.

  2. WordPress converts the shortcode attributes (i.e. the attribute name) to lowercase, so the $Field in your Edit_field() function will be empty and you should use $field instead along with 'field' => '' in your shortcode_atts() array.

However, please just avoid using extract() and use the $atts instead to access the shortcode attributes:

Note: I've applied the context attribute in this function.

function Edit_field( $atts ) {
    // Don't use extract().
    $atts = shortcode_atts( array(
        'field'   => '',
        'context' => 'view',
    ), $atts );

    if ( $atts['field'] == 'Email_1' && isset( $_POST['Edit_Email_1'] ) ) {
        return ( 'edit' === $atts['context'] ) ? 
            esc_attr( $_POST['Edit_Email_1'] ) :
            esc_html( $_POST['Edit_Email_1'] );
    }

    if ( $atts['field'] == 'Client_Description' && isset( $_POST['Edit_Client_Description'] ) ) {
        return ( 'edit' === $atts['context'] ) ?
            esc_attr( $_POST['Edit_Client_Description'] ) :
            esc_html( $_POST['Edit_Client_Description'] );
    }

    // Shortcodes should always return something.
    return ''; // .. even if it's an empty string.
}

And because the attribute names are lowercased, then just use lowercase in the shortcode like so:

<p>[Edit_field field="Email_1"]</p>
<p>[Edit_field field="Client_Description"]</p>

<input value='[Edit_field field="Email_1" context="edit"]'>
<input value='[Edit_field field="Client_Description" context="edit"]'>
Source Link
Sally CJ
  • 39.9k
  • 2
  • 26
  • 48

Shortcodes are case-sensitive, so you should:

  • Use [Edit_field Field="Email_1"]

  • And not [Edit_Field Field="Email_1"]

Because you defined the shortcode as add_shortcode('Edit_field', 'Edit_field'); — note the first Edit_field, where the f is in lowercase.

Secondly, WordPress converts the shortcode attributes to lowercase, so the $Field in your case will be undefined and therefore, you should use $field instead.

However, please just avoid using extract() and use the $atts instead to access the shortcode attributes:

function Edit_field( $atts ) {
    $atts = shortcode_atts( array(
        'field' => '',
    ), $atts );

    if ( $atts['field'] == 'Email_1' ) {
        return $_POST['Edit_Email_1'] ?? 'NA';
    }

    if ( $atts['field'] == 'Client_Description' ) {
        return $_POST['Edit_Client_Description'] ?? 'NA';
    }
}

Additionally, always check if the POST variable is actually set:

// Example for the Edit_Email_1 variable:
return isset( $_POST['Edit_Email_1'] ) ? $_POST['Edit_Email_1'] : 'NA'; // use this
return $_POST['Edit_Email_1'] ?? 'NA'; // or like I used in the above function, using PHP 7 format