I'm at an early stage of learning Wordpress (and shortcode), so bear with me:
To me, shortcodes seem like a swiss army knife of not having to use page-specific templates for everything. I like to build as many pages in the wysiwyg as possible, but often I would need some (reusable) php stuff for displaying stuff in a certain way.
Having googled a lot, it seems to me the way to do shortcodes is like:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
My question is, is it possible to put the html in a separate template-ish file? It seems wrong and verbose to put all this markup here, escape quotes, et.c. Like a template-file for a shortcode, to which the shortcode can pass some Data Transfer Object (or simply just some scoped variables). So: display in template-file, logic for finding data (to pass to said template-file) in shortcode-function (wherever it may be defined, functions.php, separate plugin, or something).
You can set-up views(php files) and then include partial views into those ones. Wordpress allows templates to be includes within other templates to ensure code reuse and its easily modifiable by child themes. You can use this function to include those
get_template_part( $slug );
However, in your case, the short code function needs to return the value to the caller function. So, this setup will not work.
For code that effects FUNCTIONALITY, put your code in a plugin.
For APPEARANCE, put your code in your theme's template files or funtions.php file.
Many beggining WP developers lump all their code into the theme's functions.php file, this is often the wrong place for it (if that code might ever get exported to another theme, for instance). Only put code specific to a specific theme in a theme's functions.php .
To get Wordpress to recognize your plugin, create a php file and start the file like this:
<?php
/*
Plugin Name: My Caption Shortcode Plugin
Description: A really cool plugin
*/
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
?>
Put this file in your plugins directory (usually, you should create a sub directory for each plugin). Plugins are usually held in /wp-content/plugins/ . Then you can activate or deactive the code as a plugin, when you go to the plugins tab in the admin menu.
Of course, this plugin won't do anything as is. Remember that plugin functionality should be hooked into Wordpress via action hooks, filters, and shortcodes. For a shortcode for instance, you'd use the function add_shortcode somewhere to let Wordpress know your function is a shortcode.
Related
I have this in my index.php file. It adds the home banner image in WordPress. I know that it is mostly generated in WordPress customizer, but I need to add an anchor tag in this section. I can't find it anywhere in the file structure.
<?php do_action('cleanblog_index_top'); ?>
I'm not able to find where cleanblog_index_top leads to. Any help would be great. Thank you!
I stumbled on this old one while looking up the docs for do_action(). The answers are brutal so I decided to provide a better answer in case anyone else stumbles here.
If a WordPress theme has something like do_action( 'example_action_hook_tag' ) somewhere in one of the template files (such as index.php, page.php or whatever) the purpose is to provide theme or plugin authors with a way to write their own custom function that they can then "hook" onto the action with the function add_action().
WordPress would then call this function any time and anywhere do_action( 'example_action_hook_tag' ) is called.
The creators of commercial themes will often litter their template files with hooks declared with do_action() to make it easier for their customers to customize their themes via functions.php or by writing a site-specific plugin.
It looks to me that this is the likely scenario that is impacting the OP. This also explains why the OP was unsuccessful in finding where this "leads to".
It would only "lead somewhere" if the OP wrote a function in the theme/child-theme functions.php or in a plugin and added the line do_action( 'cleanblog_index_top', 'name_of_ops_function' ) to hook their function onto the cleanblog_index_top. WordPress would then call their function when do_action( 'cleanblog_index_top' ) was called in index.php.
From the name of the OP's hook, cleanblog_index_top, it sounds like the theme author intended to provide a way for others to inject output at the top of the index page template.
Suppose the OP wanted <h1>Hello World</h1> to appear there.
In functions.php of a theme/child-theme the OP could add a function that echo's this out:
function op_customization() {
echo '<h1>Hello World</h1>';
}
And hook their function onto cleanblog_index_top:
add_action( 'cleanblog_index_top', 'op_customization' );
Cheers!
You should never edit the index.php file directly, for the same reason you should never edit core Wordpress files directly - the next time WP pushes an update, your changes will be overwritten (and that assumes you don't break anything). Never edit plugin files directly, same reason.
You need to look in your theme, you should only make changes to the functions.php and style.css files in your theme - unless you create a child theme and that is a topic you should Google.
Preface: I have developed a plugin, so editing theme files is out of the question.
My shortcode function:
/*** [leaderboard] shortcode with tournament_id input value ***/
function leaderboard_shortcode( $tournamentid ) {
ob_start();
include_once dirname( __FILE__ ) . '/leaderboard.php';
$output = ob_get_clean();
return $output;
}
add_shortcode( 'golf-deputy-leaderboard', 'leaderboard_shortcode' );
I tried adding in to relevant pages, however, if the shortcode is added to a page not controlled by the plugin - e.g. the home page - , the shortcode caches.
I guess my question is this: what options do I have to NOT cache the content of the shortcode, even if the page it is placed on, has caching enabled.
I have been racking my brain, the internetz, the WordPress Codex... all to no avail. I am open to any and all suggestions. Help me Stackoverflow, you're my only hope.
Cache functions start working before all possible filers&actions, and as shortcodes run inside the_content filter, it is impossible not to cache a shortcode only. I mean, when WP returns cached data, it returns static HTML data, no any theme&plugin's PHP runs in that case.
You need add that page (which has a shortcode in its content) to exclude list of your plugin. All popular cache plugins have such exclude field inside their settings.
Or another way is building custom cache solution which simply ignores caching then the_content contains any shortcode.
I want to replace the <h2 class="fusion-post-title"> tag with <h1> tag.
I've tested the regex on regex101.com and the capturing groups are there.
Is this proper way to do so? Maybe Wordpress is not triggering my add_filter()?
function replace_content($content){
$content = preg_replace('#<h2 class="fusion-post-title"(.*?)>(.*?)<\/h2>#si', '<h1 class="fusion-post-title"${1}>${2}</h1>', $content);
return $content;
}
add_filter('the_content','replace_content');
The title isn't part of the content, hence your filter doesn't work. Last time I checked a Theme Fusion theme, the function they were using for generating titles didn't apply an filters that you could hook onto, but they did suggest that there are theme options for changing the tag used. See you have a couple of options:
You can search through the theme for fusion-post-title and see what functions come up, and whether there's any filters or options that you can use. Unfortunately, as it's a premium theme I don't have a recent copy to hand to check, otherwise I'd point you in the right direction.
You can edit the theme templates directly: I'd recommend doing this to a child theme, not the theme files directly. For more information on setting up a child theme, you should refer to the codex.
Assuming you're going with option 2: once you've got your child theme set up (i.e. you've got your functions.php and styles.css), you can copy the relevant templates from the parent theme over to the child theme, and then edit them as you wish. If you take a look at Avada's single.php you'll notice something similar to:
<?php echo avada_render_post_title( $post->ID, false, '', '2' ); ?>
The last argument - 2 - is the heading level to use (i.e h2). Simply change that to 1 and you should be good to go. Or replace it entirely with your own markup:
<h1 class="post_title"><?php the_title();?></h1>
Repeat this for which ever templates are required.
After digging internet for an answer I cannot find any idea about how to deal with my issue. I think the problem is common for someone who knows PHP a little bit.
To describe the situation. For some custom WordPress plugin I've got two PHP files: ff_config.php and loantest_form.php. First file contains some configurations of plugin plus following lines:
/**--------------------------TABLE SHORTCODES-----------------------*/
function render_loantest_form() {
include(plugin_dir_path(__FILE__) . 'front/loantest_form.php');
}
add_shortcode( 'render_loantest_form' , render_loantest_form );
/**--------------------------DISPLAY PLUGIN IN FOOTER-----------------------*/
add_action('wp_footer', 'display_loantest');
function display_loantest() {
echo render_loantest_form();
}
Which I suppose rendering second file containing enqueue scripts (js/css) and whole HTML output and placing in wp_footer where it exactly is on my page.
The question is: how to change mentioned lines to allow me to place render result (loantest_form.php) in specific div / id on page (for example #sidebar-slider)?
If you want to display your shortcode in a template file,
echo do_shortcode('[render_loantest_form]');
Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
And inside the text editor
[render_loantest_form]
You can read more about do_shortcode()
Note that you need to be sure that the file load in the render function is good and that it returns the expect content.
i've written a plugin which shortcodes can easily be used in every post and page. As this plugin can be useful in a sidebar as well i want to make the text widget usable for my shortcodes.
When i googled this i found out that i can use the add_filter() function to ensure that, but this is only possible if i have access to the theme's functions.php. But as i am the creator of the plugin and not of the theme, this is not usable for me.
Does anybody know how i can make a shortcode which is introduced with a plugin usable in the widgets section?
Thanks!
Open your theme's function file.
Find a free spot after the opening php tag that isn't part of a function.
add this:
if (!is_admin())
{
add_filter('widget_text', 'do_shortcode', 11);
}
save the file and you should be all set.
Open your page in edit mode.
Select your page location and line where you want to add short code.
Add code here and update..