I'm developing a wordpress site where the posts are loaded in a popup with ajax (with Magnific Popup). The posts are using the template single.php.
This works fine, except that the header and footer are also loaded in the popup (html tags, navigation, scripts, …). I can of course delete get_header() and get_footer() from the template, but then the single post pages aren't loaded correctly via permalinks.
I tried conditional tags, but when the template is loaded with Ajax, it doesn't 'see' it's loaded on the homepage.
I think working with different templates is an option, although I have seen sites that work with the same template (like the Zoo theme on themeforest). But I couldn't figure out how it works there.
So I'm stuck here. Anybody?
The clean solution would be to use the WordPress AJAX functions.
Themes are nowadays usually split in multiple parts to reuse blocks in various places. For example, the theme twentysixteen uses get_template_part( 'template-parts/content', 'single' ); in the single.php to include the template file that shows the actual content of the file. You can use this easily to get the content of your post without header, footer etc.
First, set up the PHP part, you can just add this in the functions.php of your theme or directly in your plugin, dependant on what you are developing.
<?php
// for the admin area
add_action( 'wp_ajax_my_action', 'my_action_callback' );
// for the public area
add_action( 'wp_ajax_nopriv_my_action', 'my_action_callback' );
function my_action_callback() {
$postid = intval( $_POST['postid'] );
$post = get_post( $postid );
setup_postdata( $post );
get_template_part( 'template-parts/content', 'single' );
wp_die(); // this is required to terminate immediately and return a proper response
}
The corresponding JavaScript part:
var data = {
'action': 'my_action',
'postid': 1234
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
// on the frontend you have to set it yourself
var ajaxurl = '<?=admin_url( 'admin-ajax.php' )?>';
jQuery.post(ajaxurl, data, function(response) {
// alert('Got this from the server: ' + response);
// response will now contain your post, without header, footer, sidebars etc.
});
my_action is the identifyer for the whole process and has to be consistent between the two parts.
Documentation for WordPress AJAX support: https://codex.wordpress.org/AJAX_in_Plugins
I finally found out that this option is included in the Magnific Popup plugin: "To modify content after it’s loaded, or to select and show just specific element from loaded file, there is a parseAjax callback".
But I'll accept the above answer as I think this is an elegant way, instead of loading the whole page and only show the part needed.
Related
I'm attempting to add a shortcode wordpress sites using a custom plugin. No matter how I try to register or call this, the shortcode does not seem to render.
I am using the default 2020 theme from Wordpress to test this.
This is the code that adds the shortcode:
add_shortcode(
'test',
function () {
return 'test output';
}
);
If I manually check this on the template:
var_dump(shortcode_exists('test'));
the following is generated on a page load:
boolean true
However, if the following is added to the page content, it does not load:
[test]
I have also added the following to the theme functions.php
add_filter( 'widget_text', 'do_shortcode' );
However, as this is not within a widget (just post text) I did this to eliminate this possibility. It did not help.
Edit:
I also added the following to the page:
do_shortcode(['test']);
This produces the required output.
Every single tutorial does the above, pretty much verbatim.
What am I missing?
To be clear: Using [test] in content to show the shortcode inside post body content is the required behaviour.
Try to change the test name, suddenly it is reserved, and secondly, print the entire shortcode
print do_shortcode('[mytest]'); // [test]
<?php
/*
Plugin Name: Test
Description: La la la fa
Version: 0.1.0
*/
function question($attrs, $content = null) {
return '???';
}
add_shortcode('question', 'question');
add_action('init', function() {
d(do_shortcode('Are you sure [question]'));
});
Methods to output content with handling of shortcodes
the_content();
get_the_content();
apply_filters('the_content', $post->post_content);
Example:
add_action('init', function() {
d(apply_filters('the_content', 'Are you sure [question]')); // $post->post_content
});
This was a custom testing template, and the content of a post is not automatically parsed.
I've actually created a huge amount of custom meta data per post that I was arranging and presenting outside of the content, so I had used the $post->post_content property.
To be clear... $post->post_content is not parsed, the_content() is.
Things now work.
This is a good gotcha.
With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.
I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.
<?php if ( is_page( '3486' ) ) { exit; } ?>
exit on a line by itself kills the page indicating that the script is being called.
The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?
I could name the plugin but the question is really more generic to any plugin.
You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
function my_deregister_javascript()
{
if ( is_page('YOUR PAGE NAME') )
{
wp_deregister_script( 'WORDPRESS JS file NAME' );
}
}
Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script
i want to hide few plugins style sheets to reduce load on our Index page and categories pages. Actually we want to display plugin style sheet only on Post not on other pages.
we have used following code in plugin, but it doesn't work. please help how to use it.
if( is_single() || is_singular('post') ) wp_enqueue_style('savrix-style.css');
If you are modifying your own plugin I see no reason your code wouldn't work. The is_single() condition is not needed, and will result in the stylesheet being loaded on custom post types and other singles that you don't intend.
However your wp_enqueue_style call is incomplete, so unless you have a wp_register_style call somewhere else defining the handle and URL of the stylesheet you need to change it to something along these lines:
if (is_singular('post')) {
wp_enqueue_style('savrix-style', plugins_url('savrix-style.css', __FILE__);
}
However, I get the impression that you are actually trying to remove a stylesheet included by a thirdparty plugin. It is generally a bad idea to modify a third-party plugin, as your modifications will be lost on the next update... it is very difficult to maintain that sort of modifications in the long run.
Instead make a new plugin and modify whatever you need from there.
What you want to achieve can be accomplished by:
Create a new folder in the wp-content/plugins folder, fx. my_load_reducer.
Inside that folder create a new file called my_load_reducer.php
Paste this in the file:
<?php
/*
Plugin Name: My Load Reducer
Description: Removes unneeded and unwanted stylesheets from other plugins
Version: 0.1
*/
//Use a class to avoid conflicts
class my_load_reducer {
function __construct() {
//Hook into wp_enqueue_scripts with a high priority
add_action( 'wp_enqueue_scripts', array($this, 'deregister_styles'), 1000 );
}
function deregister_styles() {
//Check that current post is not a single post
if (!is_singular('post')) {
//deregister the stylesheet - this removes the twentyfifteen
//main stylesheet - obviously you need to substitute the handle
//of the stylesheet you actually want to remove
wp_deregister_style( 'twentyfifteen-style' );
}
}
}
//Instantiate the class
$my_load_reducer = new my_load_reducer();
Activate the plugin through the wordpress admin.
You can remove perticular plugin css on selected page.
below code is remove plugin css to other pages and display only on post pages:
/*disable loading plugin css to page and load on post page*/
add_action('wp_print_styles', 'my_deregister_styles', 99999);
function my_deregister_styles()
{
if(!is_single())
{
wp_dequeue_style('plugin-css-handle');
wp_deregister_style('plugin-css-handle');
}
}
where 'plugin-css-handle' is perticular plugin's css handle which you want to remove.
I have been thinking about developing my own theme framework for worpdress. I'd like to use jquery ui to build a bootstrap 3.0 drag and drop interface, which I already have worked out, but I can't figure out how to edit the "Pages Add New Screen" as referenced here: https://codex.wordpress.org/Pages_Add_New_Screen
Would I add files to my client side theme that affected my admin structure as well? Does anyone have any suggestions as to how to do something like this. Alot of themes these days come with these drag and drop frameworks and it would be nice, to be able to create one of my own, just need some direction on where to start editing / looking.
We add a Custom Meta Box and do our thing inside it.
There are some hooks that are not Meta Boxes and we can use to insert content into that admin page:
add_action( 'edit_form_top', function( $post )
{
echo '<h1 style="color:red">edit_form_top</h1>';
});
add_action( 'edit_form_after_title', function( $post )
{
echo '<h1 style="color:red">edit_form_after_title</h1>';
});
add_action( 'edit_form_after_editor', function( $post )
{
echo '<h1 style="color:red">edit_form_after_editor</h1>';
});
add_action( 'edit_page_form', function( $post )
{
// edit_page_form is ONLY for pages, the other is for the rest of post types
echo '<h1 style="color:red">edit_page_form/edit_form_advanced</h1>';
});
add_action( 'dbx_post_sidebar', function( $post )
{
echo '<h1 style="color:red">dbx_post_sidebar</h1>';
});
The widget_text block belongs to Advanced Custom Fields (it's a repeatable/sortable field). I'm not sure anymore, but I think it removes the Meta Box borders with CSS or jQuery.
Instead of messing with the core files of wordpress (which would be a really bad idea) I would suggest making a custom field/sidebar or a menu page to use the drag/drop function to make a page or post.
http://codex.wordpress.org/Custom_Fields
http://codex.wordpress.org/Function_Reference/add_menu_page
If you want some examples on how others have done something similar (as a plugin) you could look at the code from others and see if you can use similar techniques to add additional fields within the screen options.
http://simple-fields.com/
http://www.advancedcustomfields.com/
After updating (or maybe not) but some time before, when I'm going to category list page mysite.com/?cat=10 I'm getting not category.php template - it opens single.php Why?
I was trying to make category-10.php (just for test) but still coming to single.php.
Where I should look about my problem?
The problem was with my ajax plugin his code is :
<?php
/**
* Plugin Name: Ajax content loader
* Description: Load content througt ajax.
* Version: 0.1
*
*/
/**
* Initialization. Add our script if needed on this page.
*/
function ajax_content_init() {
global $wp_query;
// Add code to index pages.
if( !is_singular() ) {
// Queue JS
wp_enqueue_script(
'load-posts',
plugin_dir_url( __FILE__ ) . 'js/load-posts.js',
array('jquery'),
'1.0',
true
);
$wp_query->is_single = true;
}
}
add_action('template_redirect', 'ajax_content_init');
?>
Any ideas what is wrong here?
It is for mobile version, how to activate him only for mobile? not for desktop?
Download the WordPress Debug Bar Plugin. It will add a button to your wp-admin bar that says "Debug". When pressed, this button will show you the debug panel. Find the tab in this debug panel that says "WP Query", and it should show you query vars and the template file that are being used.
This should help you figure out what query vars are being registered, and which template file is really loaded.
You should read wordpress codex for template_redirect before trying to experiment things.
This action hook executes just before WordPress determines which
template page to load.
What are you actually trying is:
if( !is_singular() ) {
If page is not a singular, means is not a template page.php or single.php it should execute the code.
So when you were at category.php it executed the code and sets the global $wp_query layer objects property single to true. So whatever template you were viewing at that time it will set that page as single.
Hope it helps.