Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9024727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:05:50+00:00 2026-06-16T06:05:50+00:00

I am developing a WordPress plugin to run a certain javascript function based on

  • 0

I am developing a WordPress plugin to run a certain javascript function based on some parameters.

The first step is to include the javascript file from my server. Easy:

add_action('wp_head', 'my_plugin_head');

function my_plugin_head(){
echo '<script type="text/javascript" src="http://www.my-server.net/js/w.js" ></script>';
}

The second step is to change certain text in the WordPress body to the javascript function…something like:

 add_filter('the_content', 'plugin_text_replace');
 function plugin_text_replace($text){
 $text=preg_replace('blah', 'blah');

 return $text;
 //I Am still researching how to setup the preg_replace. 
 //It will look for something like plugin_call[1, 40, 60]
 //And Change it To jsFunction(1, 40, 60);
 //Bonus for anyone who can help me with that :)
 }

In any case, I realized that I want the my-server.net javascript included ONLY if needed (or in other words, only if the preg_replace found a match). This is problematic to me because I can’t find anyway to add a script to the <head> tag without using an action on wp_head, which does not have any reference to the body of the text.

How can I add a header ONLY if a certain preg_match is found?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:05:51+00:00Added an answer on June 16, 2026 at 6:05 am

    To add javascript the best way is using wp_enqueue_script(), like this:

    function my_scripts_method() {
        wp_enqueue_script('my_java_function');            
    }    
    
    // For use on the Front end (ie. Theme)
    add_action('wp_enqueue_scripts', 'my_scripts_method'); 
    

    Inserting code into the <body> can be achieved like this:

    function wp_body() {
      do_action( 'wp_body' );
    }
    
    add_action( 'wp_body', 'my_body_function' ) ; // 
    
    // In "header.php" place something like this after <body>:
    
    if ( function_exists('wp_body')) wp_body(); ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been developing a pretty simple WordPress plugin, and wanted to do some live
I'm developing a Wordpress plugin. There are multiple form elements, and for some reason,
I'm currently developing a Wordpress plugin. For my plugin to work I need some
I have a function for a wordpress plugin I'm developing that takes a lot
I'm developing my first Wordpress plugin, and I have one line which deletes an
I'm developing a Wordpress plugin, and I'm having some issues with $_SERVER['HTTP_HOST'] . This
I'm developing a wordpress plugin. I'm using two different custom post types, players and
I'm developing a wordpress 3.3.1 theme and I'm having troubles with the single.php file.
Hi Stack Overflow community. I'm developing a wordpress website for Satan. First of all,
I am developing a wordpress plugin and I need to have a field where

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.