I am using following wp functions to show a user profile:
add_action( 'init', 'rewrite_function' );
function rewrite_function()
{
add_rewrite_tag('%user%', '([^&]+)');
add_rewrite_rule(
'^user/([0-9]+)/?',
'index.php?user=$matches[1]',
'top'
);
};
add_action( 'template_redirect', 'rewrite_template' );
function rewrite_template()
{
if( get_query_var( 'user' ) )
{
add_filter( 'template_include', function() {
return get_template_directory() . '/user_profile.php';
});
}
};
And user_profile.php has the follwoing: (Just for the testing purpose)
<?php
/* Template Name: User profile*/
get_header();
?>
<div class="sdfsdf">
<?php
global $wp_query;
echo 'User : ' . $wp_query->query_vars['user'];
?>
</div>
<?php get_footer(); ?>
So, if a user goes to example.com/user/1, then it goes through these functions to show the user profile.
Everything works fine.
However, I am noticing some delay in loading the page. Although the user_profile.php doesn't have any content other than showing the user id, it generally takes about 1 to 2 sec. Whereas the main page takes less than 1 sec. I could use JSON to restructure the functions and paths, but I was wondering if the rewrite functions above are optimized.
Any thoughts?
Related
I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.
I try to develop a plugin which includes a file from a template. At this moment my code view is like this:
/* Add a query var for template select, and and endpoint that sets that query var */
add_action( 'init', 'wpse22543_rewrite_system' );
function wpse22543_rewrite_system() {
global $wp, $wp_rewrite;
$wp->add_query_var( 'template' );
add_rewrite_endpoint( 'kalkulator_leasingowy', EP_ROOT );
$wp_rewrite->add_rule( '^/kalkulator_leasingowy/?$',
'index.php?template=kalkulator_leasingowy', 'bottom' );
$wp_rewrite->flush_rules();
}
/* Handle template redirect according the template being queried. */
add_action( 'template_redirect', 'wpse22543_select_template' );
function wpse22543_select_template() {
global $wp;
$template = $wp->query_vars;
if ( array_key_exists( 'template', $template ) &&
'kalkulator_leasingowy' == $template['template'] ) {
global $wp_query;
$wp_query->set( 'is_404', false );
include( get_stylesheet_directory().'/kalkulator_leasingowy.php' );
exit;
}
}
function prefix_movie_rewrite_rule() {
add_rewrite_rule( 'kalkulator_leasingowy', 'index.php?template=kalkulator_leasingowy', 'top' );
}
add_action( 'init', 'prefix_movie_rewrite_rule' );
This code runs very fine and includes the template file, but my template (header.php and footer.php) by default uses a Visual Composer and when I use this code on a page, view this:
Visual Composer works good on all pages without a /kalkulator_leasingowy.
How I can include a VC into /kalkulator_leasingowy as well?
File kalkulator_leasingowy.php
<?php
get_header();
?>
<div class="main-wrapper">
<div class="container ">
<div class="row">
</div>
</div>
</div>
<?php get_footer(); ?>
I'm not really understanding where you are trying to render custom Visual Composer code since your template file doesn't have any in it.
But based on your edit, it looks like you might actually want to be using a child theme. These make it very easy to add new template files to the parent theme without editing any of the parent's code and eliminate the need for most of your complex code.
If perhaps you are injecting the Visual Composer code from somewhere else, make sure you are applying the content filters rather than just inserting or echoing to the front end.
$content = '<div id="my_custom_content">[vc shortcode contents here]</div>';
echo apply_filters('the_content', $content);
This will make sure the end content is filtered and rendered appropriately. You might read this related answer for more information.
I use the following code in my functions.php to disable the dashicons.min.css file from the frontend. It works fine, but how can I use that action only for visitors, who are not currently signed in to WordPress?
The reason is that the WordPress admin bar at the top is broken, if the dashicons css file is not available.
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
wp_deregister_style( 'dashicons' );
}
You want to use the function is_user_logged_in() which comes with WordPress. Read about it at https://developer.wordpress.org/reference/functions/is_user_logged_in/.
So your code would be:
add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {
if( !is_user_logged_in() )
wp_deregister_style( 'dashicons');
}
Hei !
I trying to implement some new script into Wordpress.
Script is not created for wordpress so I have to improvise.
I created page called auctions so link looks like:
www.example.com/auctions
Inside that page php code implementing script, so page is showing up what it's supposed to show.
But then when i'm going to some under-page (in that new script) for example:
www.example.com/auctions/add-new
Wordpress is showing 404-page not found. How to stop wordpress after /auctions/ even if page will be called:
www.example.com/auctions/something/somethingelse/someothercode
In such a way that wordpress will allways showes 'auctions' page?
you can use template_redirect hook:
add_action('template_redirect','my_redirect');
function my_redirect(){
global $wp; //global $wp object it will return us request
if ( in_array($wp->request,array('auctions','auctions/new'))) :
add_filter( 'wp_title', 'change_wp_title', 10, 2 );
status_header( 200 );
include (TEMPLATEPATH . '/my-page-template.php');
exit;
endif;
}
function change_wp_title( $title, $sep ) {
global $wp;
if(in_array($wp->request,array('auctions','auctions/new'))){
$title = get_bloginfo('name') . $sep . ' Auction';
}
return $title;
}
I'm new to Wordpress & PHP, so kindly excuse the naivety of the question, if any.
I'm building a plugin where I need to select values from the database and create a new HTML page with the values. I'm using a custom template file.
What I've done till now
Extracted the values from database
Load & display my own template in my plugin file
add_action( 'init', 'leb_add_endpoint' );
function leb_add_endpoint()
{
add_rewrite_endpoint( 'result', EP_PERMALINK );
}
add_action( 'template_include', 'leb_render_template' );
function leb_render_template($default_template) {
//some code removed for brevity
if ( ! is_singular() || !isset($wp_query->query_vars['result']) )
{
return $default_template;
}
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
$default_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $default_template;
}
The content of my-custom-template.php is as follows
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
?>
The page gets displayed without any problem. All I want is to insert $sample_result and other similar results pulled form database into my-custom-template.php
I need to generate dynamic pages based on values pulled from DB. So each time, the final page created might be different. E.g. if one hits www.example.com/sample-post/result, a page will be shown with values pulled from the DB. If one hits www.example.com/another-sample-post/result, a different page will be shown with different values. Both these pages will have the same design, only a few values will be different. This is what I'm trying to achieve.
How can I do that? Please help me. I'm stuck. :(
You need to define "result" in query vars.
Use EP_ROOT Endpoint Mask ( result/{var} is located in the root )
Inside template_include hook, you can find result value inside $wp_query object.
I've already tested the code
// Add a new var to query vars
function result_add_query_vars( $vars ){
$vars[] = 'result';
return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );
// Add endpoint
function result_add_endpoint() {
add_rewrite_endpoint( 'result', EP_ROOT );
}
add_action( 'init', 'result_add_endpoint');
// change the template
function result_render_template($template)
{
global $wp_query;
if ( array_key_exists( 'result', $wp_query->query_vars ) ) {
$result = get_query_var('result');
$new_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $new_template;
} else {
return $template;
}
}
add_action( 'template_include', 'result_render_template' );
Now you can retrieve the query var in your custom template
/*
* Template Name: My Custom Template
*/
$result = get_query_var('result');
echo $result;
Well why don't you use $wp_query Inside your my-custom-template.php
<?php
/* Template Name: My Template*/
global $wp_query;
echo '<pre>';
print_r($wp_query); // Use this in case you want to see what else do you have with you.
echo '<pre/>';
// Now you can use $wp_query to build your dynamic query at run time.
// This will allow you to perform task at run time
?>
To Retrieve Meta
If you have saved something as a meta then
<?php
$meta_values = get_post_meta( $post_id, $key, $single );
?>
To Retrieve Child Post
If you want to retrieve child posts then
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
// Do your stuff here such as below
the_title();
the_content();
endwhile;
else:
echo 'No Post Found';
endif;
?>
1) Write this function in function.php in your active template.
function leb_render_template() {
//some code removed for brevity
//$sql = 'YOUR_QUERY_CODE'
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
return $my_template;
}
add_action('wp_ajax_leb_render_template', 'leb_render_template');
add_action('wp_ajax_nopriv_leb_render_template', 'leb_render_template');
2) Call function in your custom template.
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
$result = leb_render_template();
print_r($result); // Print Your function output.
?>