-
Notifications
You must be signed in to change notification settings - Fork 7
Tutorial PHP_Function Shortcode
The [php_function] shortcode is a method to create custom shortcodes to be used within the List Content. It enables the the ability to create custom shortcodes for custom formatting, additional data, & other functions.
Important Note: One param is required with the custom php function. By default, the WP_Post always passed in the first param.
By default, the [php_function] is disabled as an added security measure, and requires a constant to be added in order for APL to even access this feature. Also, the first param MUST be a WP_Post object, followed by any other variables you add.
The following is a list you will need access to...
- FTP.
- wp-config.php file in site root directory.
- function.php file in Child Theme.
--- OR ---
- WP Dashboard
- Plugin with wp-config.php editor.
- Child Theme Editor.
The following sections will help explain how to create custom shortcodes.
In order to modify the wp_config.php, you must either have access to FTP (or cPanel Filemanager), or a plugin that is able to modify the wp-config.php file.
Modifying the wp_config.php requires access through a filemanager or FTP client, but won’t be overwritten if any sort of update was to occur.
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* @link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', false);
// ADD APL CONSTANT.
define( 'APL_ALLOW_PHP', true );
/* That's all, stop editing! Happy blogging. */
WP’s sample can be found at: trunk/wp-config-sample.php
In order to properly add functions without the risk of loosing them. A child theme must exist and be active. If you are unfamiliar with this concept, WP has a tutorial on How to Create a Child Theme.
- First, locate the child theme in
../wp-content/themes/THEME-CHILD-NAME/. Requires access to one of the following.- Access to FTP (required for creating a child theme).
- Access to Dashboard -> Theme Editor
- Add the custom PHP function. (or for tutorial purposes, add the following example).
-
Unique function name which is called by php_function shortcode ( ex. [php_function name='FUNCTION_NAME'] )
-
First param MUST exist for WP_Post object to be passed.
-
Return variable must be a string.
function custom_shortcode_example( $post ) { $return = ''; // DO STUFF HERE. $return .= $post->post_title; $return .= ' by '; $userData = get_userdata( $post->post_author ); $return .= $userData->get( $label_type[ $atts_value['label'] ] ); // STOP DOING STUFF. return $return; }
-
Which would output something similar to Hello World by John.
Last but not least, just need to add the php_function shortcode...and that's it. For the example, the following would be added...
[php_function name='custom_shortcode_example']
Would be the equivalent to...
[post_title] by [post_author]