Disabled caching for a WordPress shortcode? - php

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.

Related

Is it possible to check rendered page content in wp_enqueu_scripts?

I am trying to optimize asset loading of a plugin that I do not own. The plugin adds content to pages via shortcodes that may or may not contain conditionals that I am after. I need to somehow get the content fully rendered inside wp_enqueu_scripts and do regex to determine if assets should be loaded. Is this possible?
So far I have tried:
get_the_content() - only shows unrendered names of shortcodes.
the_content filter hook - runs after wp_enqueu_scripts so does not work.
the_content() function - actually echoes the content which is no good for just a check.
The official way to get the rendered content is applying the the_content filter to the content. This way:
$id = 0;
$content = get_the_content($id);
$content = apply_filters('the_content', $content);
All the filters that are registered will run, so also the shortcodes that have already been added. In case these are not added yet, you should revert to using javascript on the frontend to tackle the problem.

How to display result of PHP render in HTML ID

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.

Adding a PHP page to Wordpress via Plugin

Pretty much every guide ive come across for adding php pages to Wordpress involves adding it at the theme level. e.g. How to add a PHP page to WordPress?. I want to add a new public facing page to multiple sites via a plugin. I dont want to have to add this to dozens of themes. If i can add it ad the plugin level it will work for all sites.
I have this working, but i need some way of injecting this into a theme. In order to get the sidebar and stuff without having to add custom css for each theme.
Ive started by adding a rewrite rule
RewriteRule ^mypage /wp-content/plugins/myplugin/mypage.php [QSA,L]
Then page.php contains
require_once('../../../wp-blog-header.php');
get_header();
//custom php content
//get_sidebar(); // how to get this working.
get_footer();
This also works, but the problem im having is with sidebars. Some themes dont have them and others do. Not all sidebars are 30% etc. I dont know how to build the div structure here to make it work. Some themes are 100% page width, but this looks ugly when viewed on other themes that are a fixed width. I have been able to come up with some compromises, but id rather be able to do this right.
In summery my main question here is. Is it possible to call a method that will inject html into a theme page. e.g. generate_page($html);. This method will then go to page.php of the theme and inject $html into the content area of the theme.
Edit
In an attempt to dynamically inject content into an unknown theme ive done the following
global $post;
$post->post_content = "TEST PAGE content";
$post->post_title = "Page Title";
$post->post_name = "test-page";
include get_template_directory()."/page.php";
This works for some themes and not others. Some themes will display this post just fine, but others (the default wordpres twenty fifteen theme) are displaying this post and then every other post in the database after it. Im not sure where or why its pulling all of these posts, but if i can get it to stop it looks like this will be a valid solution.
Then u could try to load a specific template page in specific case.
function load_my_template( $template )
{
if( is_page() )
$template = plugin_dir_path(__FILE__) . "dir_to/my_template.php";
return $template;
}
Or change the content that is use on loading page
function load_my_content( $content )
{
global $post;
$id = $post->ID;
if( is_page() )
{
ob_start();
include plugin_dir_path(__FILE__) . "dir_to/my_template.php";
$content = ob_get_clean();
}
return $content;
}
In your __construct()
add_filter('template_include', array($this,'load_my_template') );
add_filter("the_content", array($this,"load_my_content" ) );
add_filter("get_the_content", array($this,"load_my_content" ) );
Hope that help u.
Tell me if it's not corresponding with your question.

Integrating FullPage.js as a Wordpress Plugin: Can I adjust this plugin to work along side other plugins?

I recently followed this post on integrating FullPage.js as a WordPress plugin.
I'm having trouble getting the plugin to work with other plugins - such as "All in one Event Calendar". The calendar works it just won't display on pages using FullPage.js
I understand the limitations that a plugin meant for landing pages has, but I was wondering if there was any way to edit the existing FP plugin template so that it can utilize other plugins while also taking over the theme's template?
Perhaps editing this statement?:
function fullpage_template( $original_template ) {
if ( get_post_meta( get_the_ID(), 'fullpage_js', true ) ) {
return dirname(__FILE__) . '/templates/fullpage.php';
} else {
return $original_template;
}
}
I really appreciate any assistance on this!
You might be having problems because you are using verticalCentred:true or scrollOverflow:true.
As said in the fullpage.js FAQs
My other plugins don't work when using fullPage.js
Short answer: initialize them in the afterRender callback of fullPage.js.
Explanation: if you are using options such as verticalCentered:true or overflowScroll:true of fullPage.js, your content will be wrapped inside other elements changing its position in the DOM structure of the site. This way, your content would be consider as "dynamically added content" and most plugins need the content to be originally on the site to perform their tasks. Using the afterRender callback to initialize your plugins, fullPage.js makes sure to initialize them only when fullPage.js has stopped changing the DOM structure of the site.

Wordpress shortcode with separate template/file?

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.

Categories