I have a CPT custom post type of Books. The single template being used is 'single-books.php'. However, when I make changes to this file, they don't show up in the front-end. There is no browser or server caching.
I tested this by creating a new book post with the slug 'test'. Then, I duplicated the template and renamed it 'single-books-test.php'. As expected, the new book page started loading this template. However, when I made changes to 'single-books-test.php', the new changes won't show up.
Is there any way to clear Wordpress page template cache? Any help will be highly appreciated!
Not sure if this is your case but I’ve just discovered that the template I am using (Nanospace) itself utilizes a WordPress caching mechanism and the page is only re-rendered when I change the page using the WordPress editor.
The caching mechanism consists of two PHP functions: get_transient() and set_transient().
In the particular case of the Nanospace template, I found the get_transient() call in the file wp-content/themes/nanospace/includes/frontend/class-post.php:
$output = get_transient( $cache_key );
if ( ! $output ) {
and changed it to following:
$output = false;
if ( ! $output ) {
My changes are now applied as I expected.
Related
I am writing a wordpress plugin.
I have noticed in my debug log that i get a lot of PHP errors thrown because my shortcodes are being executed on the admin edit pages and therefore the relevant data is not available because the shortcode is loading dynamic data based upon the user front end. For example i have a function:
function myFunction_availability() {
if (is_admin()) return; // tried adding this but still get the issue
$product = $this->myFunction_get_current_product();
return "<div class='product-availability'>{$product->availability}</div>";
}
Works fine from the front end, but whenever i load the edit page from admin area i get in my log:
PHP Warning: Attempt to read property "availability" on null in /home/vagrant/yodahwp/wp-content/plugins/yodah/class.yodah.php on line 1602
As you can see from my code, i tried adding is_admin() to exit out of the function if viewing an admin page (i.e. the edit post page) but this does not seem to work.
Do any wordpress whizzes have an answer for this? I am a bit surprised that shortcodes are executed on the admin edit pages, or am I missing something?!
Thanks
This is an old question. Usually, this happens when using visual builders.
One solution is to write a condition to check if the product exists.
If using woocommerce you can try:
$product = wc_get_product( get_the_ID() );
if($product){
//continue
}
In your case, you should edit your myFunction_get_current_product() method and verify there if the product exists.
I've got kind of a unique scenario that I'm trying to nail down. I'm working on a new template for a custom post type that already exists. Basically, we're replacing the single-customposttype.php file with a new one. All of that is going swimmingly, except one thing - they have one post in that custom post type that they want to keep the OLD template on.
So there's a NEW single-customposttype.php file that will work as the default single template for that CPT.
But I need ID #93 to use the OLD single-customposttype.php template. I hoped just doing single-customposttype-93.php might do the trick, but it doesn't. What's the best way to apply the other template to only one post id?
Thanks in advance!
I deal with custom template loading all the time, it's really pretty simple! Really, all you need to do is hook into the template_include hook, and override the template based on whatever conditions you want.
That hook takes a single argument, the $template file to load. You can then use any conditionals you want and force a separate file to load instead.
add_filter( 'template_include', 'custom_template_include', 99 );
function custom_template_include( $template ){
// For ID 93, load in file by using it's PATH (not URL)
if( get_the_ID() === 93 ){
// Note the file name can be ANYTHING, the WP auto-template names don't matter here
$file = get_stylesheet_directory() . '/post-id-93-custom-template.php';
// It's generally good to see if the file exists before overriding the default
if( file_exists( $file ) )
$template = $file;
}
// ALWAYS return the $template, or *everything* will be blank.
return $template;
}
It's really that simple! Inside the custom PHP file, you have access to all of the WordPress functions and such as you would with a default template.
Generally you'll want to use the file_exists() function on the template, just to make sure it's found, otherwise you'll be passing along a file that doesn't exist, and that page will not load. By checking if it exists, it will still fall back to the old template if it's not found (deleted/renamed/moved, etc)
Also, you always need to have return $template at the end, otherwise anything that uses WordPress' template system will break.
I made a quick example on a demo site:
https://xhynk.com/content-mask/policies/cookie-policy/
https://xhynk.com/content-mask/policies/use-another-template/
The policies are a custom post type, and the cookie policy loads normally. The other one is modified with the same code as above (with the name/ID changed to match), and it's loading in a simple .php file with that content in it.
So I'm trying to figure out a way to automatically lazy load all images in a blogpost on a Wordpress site for a client.
As a developer, I'd rather not use a WP plugin (like BJ Lazy Load or A3 Lazy Load), preferring instead to use a JS plugin (JQuery Lazy by eisbhr) to give me more control.
So far, all the SO solutions I've found haven't worked.
Attempt #1
I used the the_content hook to change the src to data-src. The code I used is basic.
function add_data_src_to_content($content) {
return str_replace("src=", "data-src=", $content);
};
add_filter('the_content', 'add_data_src_to_content');
While it did change the src to data-src, the browser still loads the images below the fold (which defeats the purpose of lazy loading). I surmised that the_content hook actually also uses another hook to load images in. Which is how I got to my second attempt.
Attempt #2
For my second attempt, I tried using the image_send_to_editor hook. The code:
function add_data_src($html, $id, $caption, $title, $align, $url) {
return str_replace("<img src", '<img data-src', $html);
};
add_filter('image_send_to_editor', 'add_data_src', 10, 9);
This wouldn't work on posts that are already published. So I had to re-insert the images for this code snippet to work. I was also successful at converting src to data-src! The problem: yet again, WP still loads the image (when checking the Network tab in Dev Tools), defeating the purpose of lazy loading.
At this point, I'm stumped. How do I prevent WP from loading images below the fold without using a plugin?
I've figured out what was wrong: apparently, Wordpress automatically adds an srcset and sizes attribute to all images inserted (apparently, this has been in place since WP 4.4). I figured this out when I tried playing with the image_send_to_editor hook. The JS plugin I'm using requires me to make the srcset into data-srcset and sizes into data-sizes for the lazy load to work. Applying those to the changes on the the_content hook actually worked. Final code (which worked) is:
function add_data_src_to_content($content) {
$content = str_replace("<img src=", "<img data-src=", $content);
$content = str_replace("srcset=", "data-srcset=", $content);
$content = str_replace("sizes=", "data-sizes=", $content);
return $content;
};
add_filter('the_content', 'add_data_src_to_content');
I assume you are referring to images within content entered via post/page editors and not images in say sidebar widgets or the home page (these will obviously still load).
I added your Attempt#1 to my custom site plugin, and images added via post editor were load hobbled and did not display. However; images inserted by other means e.g. shortcodes continued to be loaded and displayed unless I gave your filter function "low priority".
Try making your Attempt #1 filter function run after "everything else". add_filter('the_content', 'add_data_src_to_content', 9999);
Remove your lazy load Javascript and then clear cache and browse one of your pages and view (HTML) source. If your filter is working or partially working you will see "data-src=" attributes in your HTML instead of "src="; if so then the problem may relate to your Javascript set-up and "early" non lazy loading.
It will add 'loading=”lazy”' attribute to all 'img loading="lazy"' and 'iframe loading="lazy" iframe' tags automatically when a new post or page is created.
It is implemented in the following way:
add_filter( 'save_post', 'add_lazy_load', 10, 3 );
function add_lazy_load($post_id, $post, $update)
{
if (wp_is_post_revision($post_id))
{
return;
}
if ( ! class_exists( 'DOMDocument', false ) )
return;
remove_action('save_post', 'add_lazy_load');
$post_status = get_post_status();
Read more: https://www.plerdy.com/blog/lazy-loading-setup-wordpress/
I think that the most viable solution for lazy loading is native lazy loading. In this case, you just have to add a loading attribute to your img tags. Then, the browser will be in charge of lazy loading.
Here are the supported values for the loading attribute:
auto: Default lazy-loading behavior of the browser, which is the same as not including the attribute
lazy: Defer loading of the resource until it reaches a calculated distance from the viewport.
eager: Load the resource immediately, regardless of where it's located on the page.
It is supported by Chrome, Edge, Opera and Firefox.
https://web.dev/browser-level-image-lazy-loading/
In my plugin i have created a custom template that prints a requested sidebar. and for running the code of this template i assigned a custom page to it (by calling update_metadata) .
Is it a good idea for getting content of a specific sidebar into Ajax call ?
Now my problem is that WORDPRESS shows it in the dashboard and front page , and after searching i have not found any easy to understand solution for Hiding a page completely so only can be accessed by its id .
Can any one tell me how to do that ?
you are going about this the wrong way. You can create a function that can create anything that can be created on a wordpress page.
But if you really must you can create a page outside of the database, etc:
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
// I created a custom post type for this plugin called market -- replace post_type with whatever you want
//basically tell wordress to add a query var if sidebar is added to url.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=market','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
This will not be a page that will be in the admin section or in any query that relates to pages but someone could of course navigate to this page. But as i said above you would be better to create a function to create your sidebar. If you want a seperate file to handle the "view" you use require_once 'filename'; a file and keep your functions area free of html.
If you are creating functions in a wordpress plugin dont forget many functions may not be available until later in the load process. Use add_action() if you run into any undefined functions
edit:
you are loading wordpress before you get to the template so you have all the functions. (google wp load for more info) + get_header() / get_footer() will also load a few things like css, etc. I had a small typo in the code above, fixed that but basically what you are doing is telling wordpress if someone lands on www.example.com/sidebar to apply a query_var (rewrite rule). Wordpress will look up its saved vars (final function) and return the template assoc. The 2nd function just registers the var.
You also have wp_functions in any file you create and include in a plugin, etc hence why you can create a file that does exactly the same as this page.
This is probably a Wordpress issue more than a Friendfeed issue but could be either. I have tried to debug with Firebug, wP_DEBUG, and error_log() but no issues have appeared.
I'm trying to initalize a set of ajax-powered feeds (using Friendfeed API) in the middle sidebar on various Wordpress category pages using a conditional statement. I can get one feed to display on one page no problem but when I add the conditional, it no longer displays.
Relevant lines:
function farmball_feed() {
global $post;
if ( is_category('3')) { $category = "farmball";
} elseif ( is_category('4')) { $category = "new-redsox";
} else { $category = "red-sox-on-farmball"; }
$feed = $friendfeed->fetch_room_feed($category, null, 0+($ind*30), 30);
Here is the full plugin code: http://www.pastie.org/private/vja7eisby93e3odh628xg
I call the function within the plugin using the following line in the sidebar-[category].php template file:
<?php if (function_exists('farmball_feed')) { farmball_feed(); } ?>
Here is the full sidebar code: http://pastie.org/private/lgxsdwxja08v93pkmvz3ug
I've checked in with the WP.org forum, MIRC, WP Tavern forum, and Friendfeed forum but no solution yet. I looked into the Developing a Widget codex but this plugin uses a couple other JS files so I don't think making it a custom widget would work. I'm not great with PHP so the answer could be a simple one. Appreciate the help.
Demo site: http://farmball.com/boston
It turned out to be an issue with where the JS plugin was being initialized. We had to add a line of php code to the friendfeed file which linked to the sidebar php code. Instead of using a conditional, we created unique sidebar template files, each calling js on its own. No conditional needed.