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

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

Related

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]"); ?>

Wordpress Hook into page body

I am working on a plugin that will be used to add a customized form of Acuity Scheduling for a specific page. I want to add the scheduling form after the menu and page title on one particular page. Here is my current code:
add_action( 'template_redirect', 'check_if_acuity_page');
function check_if_acuity_page(){
if(is_page('Schedule Page')){
add_action( 'add to acuity', 'display_acuity_scheduling_api');
}
}
function display_acuity_scheduling_api(){
echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';
}
The 'add to acuity' is a custom action hook that is currently added in the header.php file of the theme I am using. It adds the schedule at the very top of the page currently, so I can at least get it on the proper page, but it is located above the Menu and Title for the page. I am working on creating a custom layout and using PHP code to modify the page depending on what the user chooses, which is why I am not just using a simple embed code.
I am new to Wordpress Plugins and Hooks so I am not sure if I am supposed to be using an action or filter hook for this. Any help would be very appreciated.
To add code just before content which is below page title use following code:
function check_if_acuity_page(){
if(is_page('Schedule Page')){
echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';}
}
function add_code_before_content($content){
$acuity_page = check_if_acuity_page();
$content = $acuity_page.$content;
return $content;
}
add_filter('the_content','add_code_before_content');
Hope this helps.
WordPress action hooks are a means of providing a way for other developers to insert their own code in specific locations within your code, in order to change or expand the functionality of your code.
So in this case you should be using an action hook.
The concept of filters and hooks is explained in this article.
So by placing the add_action function in your template after the menu and page title you can hook onto it with a function.
In your page template after the menu and page title:
add_action( 'add to acuity', 'check_if_acuity_page');
In your functions.php:
function check_if_acuity_page() {
if(is_page('Schedule Page')) {
echo '<div style="margin-top: 25px;">"Code to add Acuity Schedule to page"</div>';
}
}

WP load scripts if template has shortcode

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.

How to remove posts from admin sidebar in wordpress

I was wondering how to remove the posts section from the wordpress admin sidebar (see image below)
you will be needing to edit functions.php for this and add some code in that. This section of posts lies as edit.php
See the official wordpress codex documentation for remove_menu_page() function to understand better. Doc states function usage as:
<?php remove_menu_page( $menu_slug ) ?>
Here $menu_slug is edit.php for post menu.
Now create your own function called post_remove() and add code in functions.php as:
function post_remove ()
{
remove_menu_page('edit.php');
}
The next part is to hook your post_remove() function with a specific action which in this case is admin_menu to trigger this function. For that, add some more code in functions.php:
add_action('admin_menu', 'post_remove');
So in short, following is complete code that you need to add in your functions.php file:
function post_remove () //creating functions post_remove for removing menu item
{
remove_menu_page('edit.php');
}
add_action('admin_menu', 'post_remove'); //adding action for triggering function call
Official documentation links
http://codex.wordpress.org/Function_Reference/remove_menu_page
http://codex.wordpress.org/Function_Reference/add_action
I hope this helps! Please do me a favor - vote up for my answer and mark it accepted.
add this function to your functions.php
function remove_menu ()
{
remove_menu_page('edit.php');
}
add_action('admin_menu', 'remove_menu');

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