If is ACF block template - wordpress

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;
}
}

Related

Wordpress Plugin Custom Post Type Archive Template results in other pages not displaying

This is the code I am using to call my archive template, now the knowledgebase section works, the forums work, but all other pages throughout the website are now blank.
function kb_archive_template_function($arhive_template){
if(is_post_type_archive('knowledgebase')){
$theme_files = array('/templates/archive-knowledgebase.php');
$exists_in_theme = locate_template($theme_files, false);
if($exists_in_theme == ''){
return plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
}
You can use this code ,instead of your code
function get_custom_post_type_template( $archive_template ) {
global $post;
if ( is_post_type_archive ( 'knowledgebase' ) ) {
$archive_template = plugin_dir_path(__FILE__) . '/templates/archive-knowledgebase.php';
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
Try that, then let me know the result. Thanks

How to conditionally check a custom single post template in WordPress

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.

Wordpress filter title

recently I made a question about content filtering which went ok.
Now I'm trying to achieve the same filtering about title, but when it comes to title it doesnt filter only the_title(); but all titles within wp_nav are being filtered.
The aim is to achieve filtering single post title not the ones within the loop (I know about in_the_loop() )
<?php
class Filter_Title {
public function __construct() {
if( !is_front_page() && !is_home() && !is_single() ) return;
if( !is_singular( array('post','page') ) ) return;
add_filter( 'the_title', array(&$this, 'manage_page_title') );
}
public function manage_page_title($title) {
$title = '';
return $title;
}
}
$filtertitle = new Filter_Title();
?>
This is my mini plugin class.
I just tested this and you can match the post_ID with a param to check if its a post.
Since post ids don't match menu ids this should work. Give it a try.
function remove_post_title($title, $id) {
if (is_single() && in_the_loop() && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'remove_post_title', 10, 2);
Note: in a plugin you may need to hook the add_filter in another function and run it in wp_head like before.

target a custom page using plugin function in wordpress

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";
}

Wordpress - sidebars list

I'm trying to get a list with all registered sidebars using $wp_registered_sidebars but it returns an empty array.
Here is my code:
foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
{
$sidebar_options[$sidebar['id']] = $sidebar['name'];
}
It returns Array()
In a page template it works, but not in my functions.php
Any idea?
Thanks
put this function in your theme functions.php
function get_my_widgets()
{
foreach ($GLOBALS['wp_registered_sidebars'] as $sidebar)
{
$sidebar_options[$sidebar['id']] = $sidebar['name'];
}
}
add_action('init','get_my_widgets');
and call this function as usual as get_my_widgets(); to get the registered sidebar list
add the following function in your theme functions.php
<?php
function my_sidebar_selectbox( $name = '', $current_value = false ) {
global $wp_registered_sidebars;
if ( empty( $wp_registered_sidebars ) )
return;
foreach ( $wp_registered_sidebars as $sidebar ) :
echo $sidebar['name'];
endforeach;
}
?>
and call it as my_sidebar_selectbox(); it is working to me

Resources