• Resolved sturmy31

    (@sturmy31)


    Hi,

    I would like to use variables in Php Shortcode to be able to use parameters when calling a shortcode from WP Code Manager.
    As example :
    – Page content :

    [cmruncode name=”Generic Table Listing” tablename=”MyTable” whereclause=”1″]

    – Snippet code : something like …


    $sql = “SELECT * FROM “.$tablename.” WHERE “.$whereclause.”;”;

    Is it possible with WP code manager ?
    If yes what is exactly the syntax within the snippet ?

    Thank you.
    Roger

    PS : It works with the plugin “Post Snippet”, but I prefer to use your plugin.

    • This topic was modified 3 years, 3 months ago by sturmy31.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi Roger,

    The Code Manager does not support custom parameters. You can use URL parameters, but no custom shortcode parameters. But it looks like a good change request! ?? Let me see if I can add them…

    Thanks,
    Peter

    Thread Starter sturmy31

    (@sturmy31)

    Hi Peter,

    Do you have an example of Php Shortcode to access URL parameters, please ?
    This code could complete the Code Manager sandbox…

    Thanks in advance,
    Roger

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi Roger,

    Suppose you add URL parameter my_param to your request:
    ?my_param=test

    To get the value for my_param:

    $my_param = isset( $_REQUEST['my_param'] ) ? sanitize_text_field( $_REQUEST['my_param'] ) : null;
    if ( null !== $my_param ) {
    	// Argument available for use...
    } else {
    	// Argument available not available...
    }

    BE CAREFULL!!! If you want to use these values in your queries, make sure to sanitize.

    Don’t use this:
    $sql = “SELECT * FROM “.$tablename.” WHERE “.$whereclause.”;”;

    For the table name add at least backticks:
    $sql = “SELECT * FROM ".str_replace('‘,”,$tablename).”`;

    For the where clause you should use wpdb prepare.

    I will add the posibility to add custom parameters to your shortcode anyway. Great idea! ??

    Hope this helps,
    Peter

    Thread Starter sturmy31

    (@sturmy31)

    Thank you so much Peter !!!
    I’m aware of Sql injection.
    Thank you again even for code to sanitize ??

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi Roger,

    I just released an update of the Code Manager which supports custom shortcode parameters. The new feature is documentated here:
    https://code-manager.com/blog/docs/index/shortcodes/php-shortcodes/

    There are a few other new features. I hope you like them. I’ll try to create a small video next week to present them.

    Best regards,
    Peter

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Use of variables within PHP shortcode’ is closed to new replies.