Add Code before wp_head
-
I am trying to write my own custom plugin and need to add php script before everything else including the header of each page.
Is there a way to include code before wp_head() as I am trying to add a conditional wp_redirect to every page via the plugin.
-
As far as I know the very first hook to jump in is wp_head
A quick and dirty workaround would be to add code into the theme’s header.php file.
well i am creating a plugin not a theme and so i suppose the only way i can do it create templates that get copied across to the theme’s folder.
But surely there must be a hook I can use to insert before the header/top of the page.
I just cant find anything elegant or clean that would work.
Any more suggestions?
If you want to redirect, try the template_redirect action.
https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
You just need to enqueue your scripts before plugin does it. You can do it by setting priority to 0 for your hook. For example, do the following:
add_filter( ‘wp_enqueue_scripts’, ‘wpse8170_enqueue_my_scripts’, 0 );
// or if you enqueue your scripts on init action
// add_action( ‘init’, ‘wpse8170_enqueue_my_scripts’, 0 );function wpse8170_enqueue_my_scripts() {
wp_enqueue_script( ‘myscript’, ‘http://path/to/my/script.js’, array( ‘jquery’ ) );
// my else scripts go here…
}That was my first attempt but because the redirect is inserted at the end of the wp_head section it doesn’t work.
I has to come at the very top as well as the session variables that i am using.
So what i will probably end up doing is injecting pages/templates via the plugin into the theme which is not what i really wanted to do.
Why do you need to insert something via wp_head?
template_redirect runs before wp_head.What is it exactly you want to do?
Yes agree with keesiemeijer
This action hook executes just before WordPress determines which template page to load.
It is a good hook to use if you need to do a redirect with full knowledge of the content that has been queried.function my_page_template_redirect()
{
if( is_page( ‘goodies’ ) && ! is_user_logged_in() )
{
wp_redirect( home_url( ‘/signup/’ ) );
exit();
}
}
add_action( ‘template_redirect’, ‘my_page_template_redirect’ );Ahhh ok sorry guys I miss read exactly what it does.
Appreciate you correcting me otherwise I would have completely missed it.
Which i have screwed something up in my code instead….well at least i can narrow down the issue better.
No problem π
Good luck with the plugin
Thanks
ok so i am using this format and I get the feeling i am not using it correctly
In the plugin php file I have –
function init_web_header(){
include web_plugin_dir . ‘/pages/web-session.php’;
}
add_action(‘template_redirect’, ‘init_web_header’);Is this the correct place/usage. Sorry for all the questions.
Is web-session.php a file inside your plugin folder?
What does the file do?I don’t think template_redirect is the correct action for including files.
If you want to include that file before everything loads I would use the
init
action for that
https://codex.wordpress.org/Plugin_API/Action_Reference/initthe full path is wp-content/plugins/myplugin/pages/web-session.php’
At the moment i am just trying to get the following code to be accepted
session_start();
if (isset($_SESSION[‘auth_characterid’])) {
echo “Logged in. “.$_SESSION[‘auth_characterid’];
exit;} else {
if(is_page(‘Login’)) {
echo “not logged in”;
// deal with SSO redirect
}
}Where do you set
$_SESSION['auth_characterid']
?Is there a reason you use sessions over WordPress functions like
is_user_logged_in()
https://developer.wordpress.org/reference/functions/is_user_logged_in/
I am building a custom SSO login system which will go into a custom user area as i don’t wont to completely rewrite the wp-login system
and unfortunately add_action( ‘init’, ‘process_post’ ); doesnt appear to do anything at all, so should the add_action go into the plugin base file or somewhere else.
oh and is pulled from a custom table within the wordpress dbase
- The topic ‘Add Code before wp_head’ is closed to new replies.