添加< script>到< head>中的WordPress元素
问题描述:
我正在尝试插入以下代码:
I'm trying to insert this code:
<script type="text/javascript">
some Javascript codes comes here
</script>
在前端和管理面板中进入WordPress的<head></head>
部分
to WordPress' <head></head>
section in front end and in admin panel
例如,Joomla! 1.6具有一个允许这样做的API:
E.g., Joomla! 1.6 has an API that allows this:
$doc =& JFactory::getDocument();
$doc->addCustomTag($headTag);
我需要为不同的页面添加不同的内容.例如:
I need to add different things for different pages. For example:
第1页 我需要添加
<link rel="alternate" type="application/rss+xml" title="feed title" href="feed url" />
几页
第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>
几页
第3页 我需要添加
<link rel="stylesheet" type="text/css" href="/wordpress/wp-content/plugins/jobs/lknlibrary/js/tabs/tab.webfx.css" />
几页
我在管理面板中有超过70个以上的页面.
I have 70+ pages in admin panel like those above.
尝试通过示例管理WordPress的标题有点困难.
Trying to manage the header of the WordPress with the example is a bit difficult.
答
在主题的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' );