Include script in template_redirect page - php

I'm writting a plugin and on this must generate a suggest page, the suggest I generate with template_redirect, like so:
add_action('template_redirect', 'suggest_page');
function suggest_page() {
if (!preg_match('/suggest\.php$/', $_SERVER['REQUEST_URI']))
return;
...
}
I'm wanting to include jQuery on this page, i konw that can use wp_head(); just before </head> and then use wp_enqueue_script('jquery'); but the wp_head(); includes a lot of stuff that i don't need.
so there's a way to add a wordpress script to a page without include all head stuff of wordpress ?

from the template_redirect codex, loading a template from a plugin. have you tried this

Related

Is there any way to add tags to the <head> section in WP?

I develop a plugin and need to add meta tags to the section.
I have spent about 1 hour and had found a lot of hooks, but all of them are bypasses. I don't want to turn my code in piece of ****, can sb tell me if there is normal way to add html to the ?
Thank you
It depends on which kind of content you want to include in <head>.
Scripts and styles need to be registered and/or enqueued using the proper WP functions:
wp_enqueue_script()
wp_enqueue_style()
You can hook this functions in wp_enqueue_scripts if you need them in your front-end (as I guess from your question) or admin_enqueue_scripts to have them available in admin area.
Any other type of content could be hooked up using the wp_head action hook:
function hook_metatag() {
?>
<meta name="description" content="Description">
<?php
}
add_action('wp_head', 'hook_metatag');
This will be echoed when the theme calls the wp_head() function.
The standard is to use the wp_head hook.
<?php
add_action( 'wp_head', function() {
echo '<meta ... />';
} );
Note: This works well for meta tags. However, if you're injecting styles or scripts, instead of printing those out yourself using wp_head, use wp_enqueue_script() or wp_enqueue_style().

Wordpress/PHP: how to place meta tag at top of head

So, I have this PHP function that adds the X-UA-Compatible meta tag to the head of every page of my Wordpress site. (I'm new to Wordpress and PHP so forgive me about terminology). It's in the functions.php file of my StudioPress child theme.
The problem is the tag only works if it's placed very near the top, and the way I have it set up now causes the tag to appear halfway down, so it doesn't work. How can I force this tag to appear at the top of the head of each page?
Ex : You can use wp_head action. The wp_head action hook is triggered within the <head></head> section of the user's template by the wp_head() function. Although this is theme-dependent, it is one of the most essential theme hooks, so it is widely supported.
function aaa_custom_function() {
?>
Your Tag
<?php
}
add_action('wp_head', 'aaa_custom_function', 1000);
Use the third parameter to control the position. Change 1000 to another values to find the perfect position.
Create your custom hook like this way.
open header.php then write this below line where you want to show meta tag
<?php do_action("my_custom_metatags");?>
Now open your functions.php and write down this below code
function my_custom_metatags_fn()
{
//your tag goes here.
}
add_action("my_custom_metatags","my_custom_metatags_fn");
//Test this code it will work like a charm for you.
adding to #Damith answer wp_head action will work.
Refrence
also you can override header.php file into your child theme.
add this code in function.php
<?php function your_custom_function() {
// your custom code which you want to add in header file
}
add_action('wp_head', 'your_custom_function', 1);

Wordpress - how to add a code snippet to <head> of each page

I want to add some code to the head of each page that loads using a plugin that I build. What is the proper hook for this and the proper way of doing it? Thanks a lot
For those looking for a sample code, here it is:
add_action( 'wp_head', 'YOUR_SCRIPT_IN_HEAD_FUNCTION' );
function YOUR_SCRIPT_IN_HEAD_FUNCTION() {
echo '<script>
// some code
</script>';
}
To add JS code, use the wp_enqueue_script() function - put your call in a function which gets added to the filter 'wp_enqueue_scripts'.
To add HTML, write a function to render whatever code you need and put it onto the wp_head filter. As in add_action('wp_head', 'your_function');
Does that help?

body onLoad on a specific WordPress page

Using the latest Wordpress version (3.6), I am trying to apply:
<body onLoad="window.scroll(0, 150)">
To a specific landing page on my website so that the page scrolls to a specific point on page load.
Given the way that Wordpress is set up - with the body tags included in header.php - how can I echo the above code within the body tag for my specific page only, without applying it to the rest of my pages?
I am presuming something needs to go in here:
<body <?php body_class(); ?><?php my code in here?>>
You don't need to apply the code to the <body> tag, see: https://stackoverflow.com/a/191318/1287812
This can be done in functions.php with the following code, and using the conditional tags mentioned by SrikanthAD:
add_action( 'wp_footer', 'b5f_on_load_script' );
function b5f_on_load_script()
{
// Not our page, do nothing
if( !is_page( 'about' ) )
return;
?>
<script type="text/javascript">
window.onload = function() { alert( 'Inside specific page' ); };
</script>
<?php
};
Also see: How to use window.scroll to automatically scroll on pageload?
For some reason, I was having difficulties using the above approach, however the logic was correct.
I have now instead registered the scroll script separately and enqueued it based on page template using:
wp_register_script( 'scroll', get_template_directory_uri() . '/js/scroll.js' );
if( is_page_template('my-landing-page.php') ) wp_enqueue_script( 'scroll' );
With: window.onload=function(){window.scroll(0, 140)}; as a separate file.
Thanks for all the help.
You can use many of the conditional tags available in WordPress to get the desired functionality.
http://codex.wordpress.org/Conditional_Tags
Example:
if ( is_page( 'about' ) ) {
// do something
}
I agree with brasofilo, you don't necessarily have it include the conditional tag in your template. It can used anywhere, depending on your project.

Wordpress: Load only one jquery script on website

Just about to launch a WordPress site but have noticed that it's currently loading in two jquery files, one in wp-includes and one from my header.php, is there a way to make wordpress load the wp-include one on the front end? Done quite a bit of search and have the only mention of this seems to include the following code, but I can't find any documentation about it, any ideas?
<?php wp_enqueue_script("jquery"); ?>
As of WordPress 3.3, this is the best way to do it, using the proper hook:
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
you need to include the following code before <?php wp_head(); ?> in your header.php
<?php wp_enqueue_script("jquery"); ?>
and you can remove other jquery includes from header.php
In addition to what Aram Mkrtchyan said, you can enqueue your scripts also using wp_enqueue_script().
<?php
wp_enqueue_script('jquery');
wp_enqueue_script('your_script', "path/to/your/script.js" , array('jquery'));
?>
The third argument to wp_enqueue_script() tells WordPress that your_script is dependent on jquery, so load it only after jquery has loaded.
Actually, you need to use admin_init hook to make it work in admin section:
function jquery_for_admin() {
wp_enqueue_script('jquery');
return;
}
add_action('admin_init', 'jquery_for_admin');

Categories