Calling a custom footer in Genesis based on page template - php

I am using the Genesis framework and need to load a custom footer when a certain page template loads, but I am having issues passing the page template file name into the function. I cannot use the slug because the template can be used for multiple pages, so I really need to use the template file name instead. I believe I'm on the right path with what I have so far, but I don't quite understand what I'm reading about get_query_var and how it can help pass the value into my function.
So far my code is as follows:
remove_action( 'genesis_footer', 'genesis_do_footer' );
if (is_page_template( 'page-alternate.php' )) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
Any assistance would be greatly appreciated. Thank you

The "Is Page Template" function actually looks for the template page name not the file so in this example if the page template name was "alternate" you would have:
remove_action( 'genesis_footer', 'genesis_do_footer' );
$CurrPageTemplate = get_page_template_slug( get_queried_object_id() );
if ($CurrPageTemplate == 'page-alternate.php' ) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
See how you go with that.

Related

Wordpress Custom search page other than search.php

I want to include custom Hashtag search page in my Wordpress theme.And I have the following code in functions.php file
function hashtag_template( $template ){
global $wp_query;
$hash = get_query_var('hashtag');
if( !empty($hash) ){
return locate_template('hashtags-search.php');
}
return $template;
}
add_action( 'template_include', 'hashtag_template' );
and I have also added a hashtag-search.php template
but when I am searching with www.mysite.com/?hashtag=string, it does not shows that(hashtag-search.php) search page.Where may be the problem?

On a WordPress page template, what code can customize title and meta description?

On custom-page-template.php, what code can customize the section ( and <meta name=”description”) for all pages which are based on this template?
I tried using the following but they nothing on a page template. (They work on functions.php though).
add_filter( 'document_title_parts', 'custom_document_title_parts' );
add_action( 'wp_head', 'my_add_meta_description');
Any idea, what code can customize and in a custom-page-template.php?
Thanks.
Use this in functions.php: (change page template's name in is_page_template)
add_filter( 'document_title_parts', function( $title_parts_array ) {
if ( is_page_template('custom.php') ) {
$title_parts_array['title'] = 'New title';
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description");</script>';
}
return $title_parts_array;
} );
Or you can use this before get_header() in your custom page template
function new_page_title() {
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description2");</script>';
return 'New Title2';
}
add_action( 'pre_get_document_title', 'new_page_title' );

Add custom canonical for front page - Yoast

I want to add a custom url for the front page using YOAST. I'm trying this approach but it doesn't do anything, can you help me?
function design_canonical($url) {
global $post;
$url == ( 'www.cator.com/cator/');
}
add_filter( 'wpseo_canonical', 'design_canonical' );
any idea where is the problem?
thanks
You first need to check if your page is home or not which you can check through is_home()
function design_canonical($url) {
if (is_home()) {
return 'your-custom-url.com'; //Enter here your custom url
} else {
return $url;
}
}
add_filter( 'wpseo_canonical', 'design_canonical' );

WordPress Plugin : Expose public-view with writing url

I'm quite new to WordPress Plugin development. I'm trying to use the plugin boilerplate to create a simple plugin for learning purpose.
I want to create a "Contact Form", the view will be located in /wp-content/plugins/contact-form/public/partials/contact-form-public-display.php. And I want to access it using http://localhost/contact-form/.
My class class-contact-form-public.php :
function init_internal_rewriting()
{
add_rewrite_rule( 'contact-form$', '?page=contact-form', 'top' );
}
function rewriting_query_vars( $query_vars )
{
$query_vars[] = 'page';
return $query_vars;
}
function rewriting_parse_request( &$wp )
{
if ( array_key_exists( 'page', $wp->query_vars ) ) {
$this->display_plugin_page();
exit();
}
return;
}
public function display_plugin_page() {
include_once( 'partials/contact-form-public-display.php' );
}
When I'm hooking those function :
// Rewriting URL
$this->loader->add_action( 'init', $plugin_public, 'init_internal_rewriting');
$this->loader->add_action( 'parse_request', $plugin_public, 'rewriting_parse_request');
$this->loader->add_filter( 'query_vars', $plugin_public, 'rewriting_query_vars' );
But I can't access the css and js.
Does someone have an idea to make it clearer ? Be able to access my "public" part in my frontend website.
Thank you for your help.
Create a short code for the contact form. And past the short code into page content section.
Check the link below how to create wordpress short code.
https://codex.wordpress.org/Function_Reference/add_shortcode

How to add values to a Wordpress template file programmatically?

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.
?>

Categories