将<script>添加到<head>元素中的wordpress

tri*_*sis 48 javascript wordpress plugins

我正在尝试插入此代码:

 <script type="text/javascript">
    some Javascript codes comes here
</script>
Run Code Online (Sandbox Code Playgroud)

<head></head>前端和管理面板中的Wordpress 部分

好吧,Joomla!1.6有一个允许这样的API:

        $doc =& JFactory::getDocument();
        $doc->addCustomTag($headTag);
Run Code Online (Sandbox Code Playgroud)

我需要为不同的页面添加不同的东西.例如:

第1页 我需要添加

<link rel="alternate" type="application/rss+xml" title="feed title" href="feed url" />
Run Code Online (Sandbox Code Playgroud)

几页

第2页 我需要添加

<script type=\"text/javascript\" src=\"" . LIVE_SITE .
 "/wp-content/plugins/jobs/lknlibrary/js/ajax.js\"></script>
    <script type=\"text/javascript\">

    var ajax = new sack();
    var currentClientID=false;
    function getClientData()
    {
        var clientId = document.getElementById('db_country_id').value.replace(/[^0-9]/g,'');
        ajax.requestFile = '" .BASE_PATH . "/wp-content/plugins/jobs/com_jobs_admin/tasks/get_location_data.php?task=get_location_data&name=db_parent_id&getClientId='+clientId;    // Specifying which file to get
        ajax.onCompletion = showClientData; // Specify function that will be executed after file has been found
        ajax.runAJAX();        // Execute AJAX function
    }

    function showClientData()
    {
        clearJS = ajax.response;
        var strTagStrippedText = clearJS.replace(/(<\s*\/?\s*)div(\s*([^>]*)?\s*>)/gi ,'');
        document.getElementById('locationsDiv').innerHTML=strTagStrippedText ;
    }

    function initFormEvents()
    {
        if (document.getElementById('db_country_id')){
            document.getElementById('db_country_id').onchange = getClientData;
            document.getElementById('db_country_id').focus();
        }
    }

    window.onload = initFormEvents;
</script>
Run Code Online (Sandbox Code Playgroud)

几页

第3页 我需要添加

 <link rel="stylesheet" type="text/css" href="/wordpress/wp-content/plugins/jobs/lknlibrary/js/tabs/tab.webfx.css" />
Run Code Online (Sandbox Code Playgroud)

几页

我在管理面板中有70多个页面,如上所述.

试图用这个例子来管理WordPress的标题有点困难.

ggu*_*erg 128

在你的主题中functions.php:

function my_custom_js() {
    echo '<script type="text/javascript" src="myscript.js"></script>';
}
// Add hook for admin <head></head>
add_action( 'admin_head', 'my_custom_js' );
// Add hook for front-end <head></head>
add_action( 'wp_head', 'my_custom_js' );
Run Code Online (Sandbox Code Playgroud)


小智 11

对于任何来这里看的人,我恐怕我和@usama sulaiman在这里.

使用enqueue函数提供了一种根据脚本依赖性加载样式表和脚本的安全方法,并且是Wordpress推荐的实现原始海报试图实现的方法.想想所有试图加载自己的jQuery副本的插件; 你最好希望他们使用入队:D.

此外,尽可能创建插件; 如果您没有备份并且升级主题并在此过程中覆盖您的函数文件,则可以将自定义代码添加到您的函数文件中.

有一个插件处理这个和其他自定义函数也意味着你可以关闭它们,如果你认为他们的代码与其他一些插件或功能冲突.

您正在寻找插件文件中的以下内容:

<?php
/*
Plugin Name: Your plugin name
Description: Your description
Version: 1.0
Author: Your name
Author URI: 
Plugin URI: 
*/

function $yourJS() {
    wp_enqueue_script(
        'custom_script',
        plugins_url( '/js/your-script.js', __FILE__ ),
        array( 'jquery' )
    );
}
 add_action( 'wp_enqueue_scripts',  '$yourJS' );
 add_action( 'wp_enqueue_scripts', 'prefix_add_my_stylesheet' );

 function prefix_add_my_stylesheet() {
    wp_register_style( 'prefix-style', plugins_url( '/css/your-stylesheet.css', __FILE__ ) );
    wp_enqueue_style( 'prefix-style' );
  }

?>
Run Code Online (Sandbox Code Playgroud)

按如下方式构建文件夹:

Plugin Folder
  |_ css folder
  |_ js folder
  |_ plugin.php ...contains the above code - modified of course ;D
Run Code Online (Sandbox Code Playgroud)

然后将其压缩并使用您的添加插件界面将其上传到您的Wordpress安装,激活它并且Bob是您的叔叔.


djj*_*eck 5

详细说明上一个答案,您可以在输出标题之前收集所有必需的片段,然后使用动作挂钩将所有需要的内容注入头部.

在你的functions.php文件中,添加

$inject_required_scripts = array();

/**
 * Call this function before calling get_header() to request custom js code to be injected on head.
 *
 * @param code the javascript code to be injected.
 */
function require_script($code) {
  global $inject_required_scripts;
  $inject_required_scripts[] = $code; // store code snippet for later injection
}

function inject_required_scripts() {
  global $inject_required_scripts;
  foreach($inject_required_scripts as $script)
    // inject all code snippets, if any
    echo '<script type="text/javascript">'.$script.'</script>';
}
add_action('wp_head', 'inject_required_scripts');
Run Code Online (Sandbox Code Playgroud)

然后在您的页面或模板中,使用它

<?php
/* Template Name: coolstuff */

require_script(<<<JS
  jQuery(function(){jQuery('div').wrap('<blink/>')});
JS
);

require_script(<<<JS
  jQuery(function(){jQuery('p,span,a').html('Internet is cool')});
JS
);

get_header();
[...]
Run Code Online (Sandbox Code Playgroud)

我为javascript做了这个,因为它是最常用的,但它可以很容易地适应头部中的任何标记,并使用内联代码或通过将href/src传递给外部URL.