WP load scripts if template has shortcode - wordpress

I have a plugin, it has many css theme files. Of course I do not want to load all of them, only one. It depends on config. For post I use has_shortcode function, but how todo the same thing with template that use do_shortcode function.
Note:
I found a good solution, I use
$this->loader->add_action( 'init', $plugin_public, 'register_scripts');
$this->loader->add_action( 'wp_footer', $plugin_public, 'print_scripts');
Inside shortcode handle I set a global var to true
global $imagelink_plugin_shortcode_used;
$imagelink_plugin_shortcode_used = true;
The function print_scripts add my scripts if my global var is true
public function print_scripts() {
global $imagelink_plugin_shortcode_used;
if ( ! $imagelink_plugin_shortcode_used )
return;
wp_print_scripts($this->plugin_name . '-imagelinks');
}
Thanks for answers.

Instead of using has_shortcode function, what you can do is register and enqueue those files when that shortcode is rendered.
First, register your css files with wp_register_script. Make sure to hook this into wp_enqueue_scripts
Now, inside your shortcode function, enqueue the files.
wp_enqueue_style('theme-css')
Using this way you can use the shortcode anywhere you want and the script is loaded only when shortcode is present on the page whether it is on content or template.

Related

Get current page id and page title inside the theme functions.php

Is it possible to get the current page id and page title inside the current themes functions.php?.
Note: I'm not referring post id.
I have tried following functions inside the themes->twentytwentytwo->functions.php file and did not work.
prin_r(the_title());
print_r(get_queried_object_id());
print_r(get_the_title());
From what I understand functions.php is included too early to use $post or get_queried_object_id().
What I've done before is create a function in functions.php that you can call in your template and pass it the id or the title.
For instance in your functions.php:
function myPage($pageId, $pageTitle) {
//Do whatever you want and return the result
}
Then in your template just call the function:
<?php
$myPage(get_the_ID(), get_the_title());
?>
Hope it helps

Wordpress - Is it possible to do a request to the wordpress database from a page html block?

I am new to WordPress.org and I figured out that I can add HTML, CSS and Javascript to a page via a code block. But my question is, can I also make a call to the WordPress database from a page? Or do I need to make a plugin for that?
You can do this with a shortcode that calls a php function in your (child-themes) functions.php.
This is a good guide to start from:
https://www.wpbeginner.com/wp-tutorials/how-to-add-a-shortcode-in-wordpress/
// function that runs when shortcode is called
function wpb_demo_shortcode() {
// Things that you want to do.
$message = 'Hello world!';
// Output needs to be return
return $message;
}
// register shortcode
add_shortcode('greeting', 'wpb_demo_shortcode');
And then call it within your post/page (in a Gutenberg shortcode block)
[greeting]
This is a good way because you can re-use and maintain it easily.
Tom
Using page you can call database you can do it using
Creating a template and assign that template in wordpress page
Or you can create short code in function.php and add shortcode in your wordpress page
Create page template
1) Create file in your theme folder and name it 'custom.php' you can name it anything.
2) Open file and add this
3) Start editing in that page you can call database using
global $wpdb;
$result = $wpdb->get_results('SELECT * FROM wp_posts LIMIT 10');
4) After editing 'custom.php' you can assign this template in your custom page
note: please refer this link to know more about page template
https://www.cloudways.com/blog/creating-custom-page-template-in-wordpress/
Create short code in function.php
1) You can create shortcode using
// function that runs when shortcode is called
function wpb_demo_shortcode() {
global $wpdb;
$result = $wpdb->get_results('SELECT * FROM wp_posts LIMIT 10');
return $result;
}
// register shortcode
add_shortcode('get_post_shortcode', 'wpb_demo_shortcode');
2) you can call shortcode in any page of wordpress.
<?php echo do_shortcode("[get_post_shortcode]"); ?>

How to create and call custom function at Theme's function.php for a plugin

I need to create a custom function for plugin wp-estore . Function should be added on function.php of Theme and should be called at plugin.
Plugins are loaded before the theme so the function hasn't been defined at the time you're calling it. Execute your code on a hook such as init instead.
function wpse_my_plugin_init() {
myPreviouslyUndefinedThemeFunction();
}
add_action( 'init', 'wpse_my_plugin_init' );

How do I register a stylesheet inside a WordPress widget?

I'm developing a WordPress widget following Dave Clements tutorial. It works well. Now I want to add some styles to it. I want the styles to be in an extra css file which will be loaded during runtime. I'm calling this function
function myprefix_add_my_stylesheet() {
wp_register_style( 'myprefix-style', plugins_url('mystyle.css', __FILE__) );
wp_enqueue_style( 'myprefix-style' );
}
right before "// Widget output //" using this statement.
add_action( 'wp_enqueue_scripts', 'myprefix_add_my_stylesheet' );
But nothing seems to happen. What am I doing wrong?
Your wp_enqueue_scripts action do not work because you call it too late, in widget() function (widget output). It's too late because Wordpress already print/send head of page.
For example, you can add this action on widget construct function.

How can I use add_filter for a specific page template?

In Wordpress I have a page template called designers.php.
When loading, it reads the slug to get a uniqe ID, then calls the DB to retrieve designer information.
I want to use this information to alter the page title, using the designer name in the title tag.
I've tried using the add_filter in my designers.php file, but it's not working:
add_filter('wp_title', 'set_page_title');
function set_page_title($title) {
global $brand;
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
}
I'm, guessing the add_filter must either be located inside a plugin or in functions.php file.
How can I achieve what I'm trying to do?
UPDATE
The function is never fired as long as I use wp_title. If I change it to init (for testing), the function is fired.
So why does the add_filternor work for wp_title?
You are almost right. The filter must reside in function.php, and is called to modify the title. You can add the filter conditionally. Use this function is_page_template() to determine if wordpress is rendering your template
Try to modify your function like this:
add_filter('wp_title', 'set_page_title');
function set_page_title($title) {
global $brand;
if (is_page_template('designer.php'))
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
else
return $title;
}
First of all add_filter must either be located inside a plugin or in functions.php file.
Then, maybe you have to set the priority for the filter :
add_filter('wp_title', 'set_page_title',1);
function set_page_title() {
global $brand;
return 'Designer '.$brand['name'].' - '.get_bloginfo('name');
}
And check the <title> meta in your header.php theme.
<title><?php wp_title(); ?></title>

Resources