i created a custom page named custom_page.php in wordpress and use this as a page template.
i wanted to create my own functions and only target my custom_page.php. this function will be installed as a plugin.
what kind of WP HOOKS should i use, for example i have this code below
function your_function() {
if ( is_page_template('custom_page.php') ) {
echo '<p>This is inserted at the bottom</p>';
{
}
add_action('wp_footer', 'your_function');
i want to only execute this code in my custom_page.php footer area.
[EDIT]
You can use these hooks for page templates.
http://adambrown.info/p/wp_hooks/hook/page_template
Or may be something like this...
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'custom_page.php' == basename( $template ) ) {
// do some things here by calling functions, hooks etc..
}
return $template;
}
If you want to just echo "Hello World" :
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'pagetemplete.php' == basename( $template ) ) {
add_action( 'the_content', 'echofunction' );
}
return $template;
}
function echofunction() {
echo "Hello World";
}
Related
I'm using ACF to build blocks.
Here's a simple function I've written via functions.php:
<?php
function my_test_function() {
if ( is_page_template( 'template-parts/blocks/hero/hero.php' ) ) {
echo '<p>I appear via the hero.php template.</p>';
}
if ( is_page_template( 'template-parts/blocks/video/video.php' ) ) {
echo '<p>I appear via the video.php template.</p>';
}
}
?>
Here's how I call the function via hero.php and video.php
<?php
my_test_function();
?>
When I test this, nothing appears. I'm guessing the is_page_template function doesn't work in this instance as it's technically not a page template.
Is there another function that might work with ACF block templates?
You could use parse_block() : https://developer.wordpress.org/reference/functions/parse_blocks/ then loop through blocks to check if blockName exist for this page :
function check_if_block_exist($block_handle) {
$post = get_post();
if(has_blocks($post->post_content)) {
$blocks = parse_blocks($post->post_content);
foreach( $blocks as $block ) {
if($block['blockName'] === $block_handle) {
return true;
}
}
return false;
}
}
I have some functions to override in my child theme but keep on getting Cannot redeclare langs_content().
I used quite a few resources to troubleshoot and something that shoud be straight forward I can't work it out
<?php
function enqueue_parent_theme_style() {
wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' );
function enqueue_child_theme_style() {
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri().'/style.css' );
}
//require_once( get_stylesheet_directory() . '/functions.php');
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_style', 100 );
if ( ! function_exists( 'langs_content' ) ) {
/*
* My Awesome function is awesome
*
*/
function langs_content( $atts, $content = null ) // shortcode lang
{
$lang = the_lang();
extract( shortcode_atts( array(), $atts ) );
$langs_content = '';
if(in_array($lang, $atts))
{
$langs_content .= do_shortcode($content);
}
return $langs_content;
}
add_shortcode('lang', 'langs_content');
}
?>
Really simple ! Remove the shortcode from init hook then add your new one ;-)
Like this :
/******
* Inside parent functions.php (don't touch this!)
******/
function test_shortcode() {
return "Hello World from parent theme";
}
add_shortcode('test_shortcode', 'test_shortcode');
/******
* Inside child functions.php
******/
function child_test_shortcode()
{
return "Hello World from child theme";
}
function remove_parent_theme_shortcodes()
{
remove_shortcode('test_shortcode');
// Add our new function with the same shortcode
add_shortcode('test_shortcode', 'child_test_shortcode');
}
// Remove the shortcode with init hook
add_action('init', 'remove_parent_theme_shortcodes');
We created a plugin with a new template and we want to hook the main theme function and return the main plugin function.
We tried it with:
add_filter( "page_template", "test" );
function test( $template ) {
if( 'plugin_name.php' == basename( $template ) )
$template = WP_PLUGIN_DIR . '/plugin_folder/plugin_name.php';
return $template;
}
and changed page template in theme functions with main function of plugin which runs template inside plugin:
add_filter( "page_template", "main_plugin_function" );
Is page_template the right filter to change theme template?
Thanks for help!
I think you should use template_include filter, this filter hook is executed immediately before WordPress includes the predetermined template file. This can be used to override WordPress's default template behavior.
For example
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
I am using the following function to call a custom single post overriding the default one single.php
function call_different_single_post($single_template)
{
global $post;
$path = get_stylesheet_directory() . '/includes/templates' . '/';
$single_template = $path. 'single-1.php';
return $single_template;
}
add_filter('single_template', 'call_different_single_post');
It's calling the single-1.php template for the single posts.
Now I want to check this template conditionally so that I can call some other js files like
function call_cust_js_single(){
if( /*..this is single-1.php template..*/){
wp_register_script('cust-js', get_stylesheet_directory_uri() . 'custom.js', false, '1.0.0');
wp_enqueue_script('cust-js');
}
}
add_action('wp_enqueue_scripts', 'call_cust_js_single');
There's a post similar to this but it doesn't quite cover your request. Here's the link here
Here's the valuable code:
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
function get_current_template( $echo = false ) {
if( !isset( $GLOBALS['current_theme_template'] ) )
return false;
if( $echo )
echo $GLOBALS['current_theme_template'];
else
return $GLOBALS['current_theme_template'];
}
Now you have the function get_current_template that will return the filename of the template file.
Now edit your header.php file, here's an example:
<?php if('single-1.php' == get_current_template())
{
//load your scripts here
} ?>
It's not as clean as what you were looking for but I think it will help you.
I've tried the following code, which im using successfully with product thumbnails but for categories haven't found a way to do it.
add_action( 'init', 'mw_replace_woocommerce_placeholders' );
function mw_replace_woocommerce_placeholders() {
add_filter('woocommerce_placeholder_img', 'custom_woocommerce_placeholder_img');
function custom_woocommerce_placeholder_img( $src ) {
$src = '<span class="thumb-placeholder"><i class="icon-camera"></i></span>';
return $src;
}
}
Any tips?
This should work
add_action( 'init', 'custom_placeholder' );
function custom_placeholder() {
if ( is_product_category() ){
add_filter('woocommerce_placeholder_img_src','custom_placeholder_img');
function custom_placeholder_img($src) {
$upload_dir = wp_upload_dir();
$uploads = untrailingslashit( $upload_dir['baseurl'] );
$src = $uploads . '/your/directory/custom_placeholder.jpg';
return $src;
}
}
}
Or you could replace the placeholder located at "wp-content/plugins/woocommerce/assets/images/placeholder.png" with your own.
The line:
if ( is_product_category() ){
in functions.php makes the theme reliant on the plugin, so if you deactivate woocommerce for testing, it breaks the site. At least that's what happened to me.