Reputation: 101
Hi can someone please tell me what I am doing wrong? I am trying to add the google analytics as per instructions on multiple sources but it keeps crashing my site no matter where I put the information Here is the code as I tried this last time for the 7th time., Below is the contents of the function.php code:
<?php
add_action('wp_head', 'wpb_add_googleanalytics');
function wpb_add_googleanalytics() { ?>
// Paste your Google Analytics code from Step 4 here
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx');
</script>
function denRegScript(){
wp_enqueue_style( 'datatable-css', get_theme_file_uri('/assets/css/datatables.css'), false );
wp_enqueue_script( 'datatables-js', get_theme_file_uri('/assets/js/datatables.min.js'), array('jquery'), '0.1', true );
}
add_action('wp_enqueue_scripts','denRegScript');
add_action('wp_ajax_movie_datatables', 'datatables_server_side_callback');
add_action('wp_ajax_nopriv_movie_datatables', 'datatables_server_side_callback');
//add_action('wp_ajax_nopriv_movie_datatables', 'datatables_server_side_callback');
function datatables_server_side_callback()
{
header("Content-Type: application/json");
$request = $_GET;
$json_pre_result = newfunc($request);
if(isset($json_pre_result)) {
echo json_encode($json_pre_result);
}
wp_die();
}
function newfunc($request){
global $wpdb;
// $catnamewithid = $wpdb->get_results(
// "SELECT id_category,category_title FROM {$wpdb->prefix}expert_jobs_categories", OBJECT_K
// );
$data = array();
$maincatParam = $request['maincat'];
$subcatParam = $request['subcat'];
error_log($maincatParam);
error_log($subcatParam);
$regionParam = $regionParamOrig = $request['region'];
$regionParam = explode(", ",$regionParam);
if(isset($regionParam[0]) && isset($regionParam[1])){
$regionParamCity = $regionParam[0];
$regionParamstate = $regionParam[1];
}else{
$regionParamCity = $regionParamstate = $regionParamOrig;
}
$empsql = "";
if($regionParamCity!="all"){
// find the employer ids
$employeeresults = $wpdb->get_results(
"SELECT id_professional FROM {$wpdb->prefix}expert_jobs_professionals WHERE `pro_address` LIKE '%, $regionParamstate%' OR `pro_address` LIKE '%$regionParamCity%' ", ARRAY_A
);
if(!empty($employeeresults)){
$te3 = array();
foreach ($employeeresults as $value) {
array_push($te3,$value['id_professional']);
}
$subEmpString = join(",",$te3);
$empsql = "AND id_employer IN ($subEmpString)";
}
}
if($maincatParam==-1){
$allMainCats = $wpdb->get_results(
"SELECT id_category,category_title FROM {$wpdb->prefix}expert_jobs_categories WHERE id_parent IN (0) ", OBJECT_K
);
// need to return all data
}else{
$allMainCats = $wpdb->get_results(
"SELECT id_category,category_title FROM {$wpdb->prefix}expert_jobs_categories WHERE id_category IN ($maincatParam) ", OBJECT_K
);
}
$superFArray = array();
foreach ($allMainCats as $maincategoryid => $maincategorynameArray) {
$subcatids = array();
if($subcatParam==-1){
$subcategries = $wpdb->get_results(
"SELECT id_category,category_title FROM {$wpdb->prefix}expert_jobs_categories WHERE id_parent IN ($maincategoryid) AND id_category NOT BETWEEN 123 AND 198 ", OBJECT_K
);
}else{
$subcategries = $wpdb->get_results(
"SELECT id_category,category_title FROM {$wpdb->prefix}expert_jobs_categories WHERE id_category IN ($subcatParam) AND id_category NOT BETWEEN 123 AND 198 ", OBJECT_K
);
//$subcategries = getSingleSubCat($subcatParam);
}
$tempar1 = array();
// find wage for all sub category id of this main category and assign sub cat id as key, assign wages as values
$implodedsubcatid = "";
if(!empty($subcategries)){
$fored = array();
$te3 = array();
$budgetarrayforcat = array();
foreach ($subcategries as $subcategoryid => $subcategoryname) {
array_push($te3,$subcategoryid);
}
$implodedsubcatid = join(",",$te3);
// Hourly
$jobsresults2 = $wpdb->get_results(
"SELECT id_category,job_budget FROM {$wpdb->prefix}expert_jobs_job WHERE id_category IN ($implodedsubcatid) AND job_payment_type='Per hour' $empsql ORDER BY job_budget LIMIT 100 ", ARRAY_A
);
if(!empty($jobsresults2)){
// make new array of obj_k
foreach ($jobsresults2 as $key => $jobline) {
$catid = $jobline['id_category'];
$rawbudget = $jobline['job_budget'];
$averageofone = 0;
$explodeall = explode(" ",$rawbudget);
if (($key = array_search("00", $explodeall)) !== false) {
unset($explodeall[$key]);
}
if(count($explodeall)) {
$a = array_filter($explodeall, function($explodeall) { return is_numeric($explodeall) == true; });
//$a = array_filter($explodeall);
$averageofone = array_sum($a)/count($a);
}
$budget = $averageofone;
$fored[$catid]['hour'][] = $budget;
}
// foreach ($fored as $subcatid => $job_budget) {
// $budg = $job_budget['job_budget'];
// $budgetarrayforcat[$subcatid]['hour'][] = $budg;
// }
}
// Week
$jobsresults = $wpdb->get_results(
"SELECT id_category,job_budget FROM {$wpdb->prefix}expert_jobs_job WHERE id_category IN ($implodedsubcatid) AND job_payment_type='Weekly' $empsql ORDER BY job_budget LIMIT 100 ", ARRAY_A
);
if(!empty($jobsresults)){
// make new array of obj_k
foreach ($jobsresults as $key => $jobline) {
$catid = $jobline['id_category'];
$rawbudget = $jobline['job_budget'];
$averageofone = 0;
$explodeall = explode(" ",$rawbudget);
if (($key = array_search("00", $explodeall)) !== false) {
unset($explodeall[$key]);
}
if(count($explodeall)) {
$a = array_filter($explodeall, function($explodeall) { return is_numeric($explodeall) == true; });
//$a = array_filter($explodeall);
if(count($a)!=0){
$averageofone = array_sum($a)/count($a);
}
}
$budget = $averageofone;
$fored[$catid]['week'][] = $budget;
}
// foreach ($fored as $subcatid => $job_budget) {
// $budg = $job_budget['job_budget'];
// $budgetarrayforcat[$subcatid]['hour'][] = $budg;
// }
}
$newfored = array();
foreach ($fored as $catid => $value) {
foreach ($value as $daytype => $average) {
if(count($average)!=0){
$sav = array_sum($average)/count($average);
}else{
$sav = 0;
}
switch ($daytype) {
case "month":
$newfored[$catid]['month'] = $sav;
//$averageString .= "<span class='fixwl'>Monthly: </span>".$average."<br/>";
break;
case "week":
$newfored[$catid]['week'] = $sav;
//$averageString .= "<span class='fixwl'>Weekly: </span>".$average."<br/>";
break;
case "hour":
$newfored[$catid]['hour'] = $sav;
//$averageString .= "<span class='fixwl'>Hourly: </span>".$average."<br/>";
break;
default:
}
}
}
}
foreach ($subcategries as $subcategoryid => $subcategoryname) {
$wages = array();
if(isset($newfored[$subcategoryid])){
$wages = $newfored[$subcategoryid];
}
$subcategoryname->wages = $wages;
$tempar1[$subcategoryid] = $subcategoryname;
}
$maincategoryname = $maincategorynameArray->category_title;
$superFArray[$maincategoryname] = $tempar1;
}
foreach ($superFArray as $maincatname => $subarray) {
foreach ($subarray as $subvalue) {
if(!empty($subvalue->wages)){
$nestedData = array();
$subcatname = $subvalue->category_title;
$averageString = '';
foreach ($subvalue->wages as $daytype => $average) {
$average = money_format('$%i', $average);
$breakweek = false;
if(isset($subvalue->wages['hour']) && isset($subvalue->wages['week']) ){
if($subvalue->wages['hour']>$subvalue->wages['week']){
$breakweek = true;
}
}
switch ($daytype) {
case "month":
$averageString .= "<span class='fixwl'>Monthly: </span>".$average."<br/>";
break;
case "week":
if(!$breakweek){
$averageString .= "<span class='fixwl'>Weekly: </span>".$average."<br/>";
}
break;
case "hour":
$averageString .= "<span class='fixwl'>Hourly: </span>".$average."<br/>";
break;
default:
}
}
$nestedData[] = $maincatname;
$nestedData[] = $subcatname;
$nestedData[] = $averageString;
$data[] = $nestedData;
}
}
}
$final_data = array(
"draw" => intval($request['draw']),
"recordsTotal" => count($data),
//"recordsFiltered" => 12,
"data" => $data
);
return $final_data;
}
function get_pre_json_time_sheets($request){
$columns=array(
0=>'name',
1=>'date',
2=>'starttime',
3=>'endtime',
);
// $posts_per_page = $request['length'];
// $doffset = $request['start'];
// $order = $request['order'][0]['dir'];
// $orderby = $columns[$request['order'][0]['column']];
global $wpdb;
$data = array();
$maincatParam = $request['maincat'];
$subcatParam = $request['subcat'];
$regionParam = $regionParamOrig = $request['region'];
$regionParam = explode(", ",$regionParam);
if(isset($regionParam[0]) && isset($regionParam[1])){
$regionParamCity = $regionParam[0];
$regionParamstate = $regionParam[1];
}else{
$regionParamCity = $regionParamstate = $regionParamOrig;
}
$subEmpString = "";
$empsql = "";
if($regionParamCity!="all"){
// find the employer ids
$employeeresults = $wpdb->get_results(
"SELECT id_professional FROM {$wpdb->prefix}expert_jobs_professionals WHERE `pro_address` LIKE '%, $regionParamstate%' OR `pro_address` LIKE '%$regionParamCity%' ", ARRAY_A
);
if(!empty($employeeresults)){
$te3 = array();
foreach ($employeeresults as $value) {
array_push($te3,$value['id_professional']);
}
$subEmpString = join(",",$te3);
$empsql = "AND id_employer IN ($subEmpString)";
}
}
$nestedData = array();
$budgetarrayforcat = array();
$maincatbudgetarray = array();
$catnamewithid = $wpdb->get_results(
"SELECT id_category,category_title FROM {$wpdb->prefix}expert_jobs_categories", OBJECT_K
);
//error_log(print_r($catnamewithid,true));
if($maincatParam==-1){
$allMainCat = getIMainCat();
// need to return all data
}else{
$allMainCat = getSingleMainCat($maincatParam);
}
if(!empty($allMainCat)){
foreach ($allMainCat as $singlemain) {
$maincatid = $singlemain->id_category;
$maincatname = $singlemain->category_title;
if($subcatParam==-1){
$subcatids = getsubcatidsStringOfMainCat($maincatid);
}else{
$subcatids = $subcatParam;
}
if(!empty($subcatids)){
// Week
$jobsresults = $wpdb->get_results(
"SELECT job_budget,id_category FROM {$wpdb->prefix}expert_jobs_job WHERE id_category IN ($subcatids) AND job_payment_type = 'Weekly' $empsql ORDER BY job_budget LIMIT 12 ", ARRAY_A
);
//(job_budget like '%per week%' OR job_budget like '%week%')
if(!empty($jobsresults)){
foreach ($jobsresults as $key => $jobline) {
$budg = $jobline['job_budget'];
$idcat = $jobline['id_category'];
if(isset($catnamewithid[$idcat]->category_title)){
$idname = $catnamewithid[$idcat]->category_title;
$budgetarrayforcat[$idname]['week'][] = $budg;
}
}
}
// Hourly
$jobsresults2 = $wpdb->get_results(
"SELECT job_budget,id_category FROM {$wpdb->prefix}expert_jobs_job WHERE id_category IN ($subcatids) AND job_payment_type='Per hour' $empsql ORDER BY job_budget LIMIT 12 ", ARRAY_A
);
//(job_budget like '%per hour%' OR job_budget like '%hourly%' )
if(!empty($jobsresults2)){
foreach ($jobsresults2 as $key => $jobline) {
$budg = $jobline['job_budget'];
$idcat = $jobline['id_category'];
if(isset($catnamewithid[$idcat]->category_title)){
$idname = $catnamewithid[$idcat]->category_title;
$budgetarrayforcat[$idname]['hour'][] = $budg;
}
}
}
// Anually
// $jobsresults3 = $wpdb->get_results(
// "SELECT job_budget,id_category FROM {$wpdb->prefix}expert_jobs_job WHERE id_category IN ($subcatids) AND (job_budget like '%per month%' OR job_budget like '%monthly%') $empsql LIMIT 12", ARRAY_A
// );
// if(!empty($jobsresults3)){
// foreach ($jobsresults3 as $key => $jobline) {
// $budg = $jobline['job_budget'];
// $idcat = $jobline['id_category'];
// if(isset($catnamewithid[$idcat]->category_title)){
// $idname = $catnamewithid[$idcat]->category_title;
// $budgetarrayforcat[$idname]['month'][] = $budg;
// }
// }
// }
$maincatbudgetarray[$maincatname] = $budgetarrayforcat;
}
}
}
//error_log(print_r($maincatbudgetarray,true));
echo '<pre>';
print_r($maincatbudgetarray);
echo '</pre>';
foreach ($maincatbudgetarray as $maincatname => $subcatarray) {
foreach ($subcatarray as $subcatname => $wagearray) {
$nestedData = array();
// we have week and hour array here in $wagearray
$averageString = "";
foreach ($wagearray as $daytype => $singlewage) {
$countjobs = count($singlewage);
$allave = array();
foreach ($singlewage as $rawtext) {
$averageofone = 0;
$explodeall = explode(" ",$rawtext);
if (($key = array_search("00", $explodeall)) !== false) {
unset($explodeall[$key]);
}
if(count($explodeall)) {
$a = array_filter($explodeall, function($explodeall) { return is_numeric($explodeall) == true; });
$a = array_filter($explodeall);
$averageofone = array_sum($a)/count($a);
}
array_push($allave,$averageofone);
}
$average = money_format('$%i', array_sum($allave)/$countjobs);
switch ($daytype) {
case "month":
$averageString .= "<span class='fixwl'>Monthly: </span>".$average."<br/>";
break;
case "week":
$averageString .= "<span class='fixwl'>Weekly: </span>".$average."<br/>";
break;
case "hour":
$averageString .= "<span class='fixwl'>Hourly: </span>".$average."<br/>";
break;
default:
}
}
$nestedData[] = $maincatname;
$nestedData[] = $subcatname;
$nestedData[] = $averageString;
$data[] = $nestedData;
}
}
$final_data = array(
"draw" => intval($request['draw']),
"recordsTotal" => count($data),
"recordsFiltered" => 12,
"data" => $data
);
return $final_data;
}
function getsubcatidsStringOfMainCat($maincat){
global $wpdb;
$results = $wpdb->get_results(
"SELECT id_category FROM {$wpdb->prefix}expert_jobs_categories WHERE id_parent IN ($maincat) AND id_category NOT BETWEEN 123 AND 198", ARRAY_A
);
if(!empty($results)){
$te2 = array();
foreach ($results as $value) {
array_push($te2,$value['id_category']);
}
$subCatString = join(",",$te2);
}
//error_log(print_r($subCatString,true));
return $subCatString;
//wp_die();
}
function getIMainCat(){
global $wpdb;
$results = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}expert_jobs_categories WHERE id_parent IN (0) "
);
return $results;
//wp_die();
}
function getSingleMainCat($catid){
global $wpdb;
$results = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}expert_jobs_categories WHERE id_category IN ($catid) "
);
return $results;
//wp_die();
}
function getSingleSubCat($catid){
global $wpdb;
$results = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}expert_jobs_categories WHERE id_category IN ($catid) "
);
return $results;
//wp_die();
}
add_action('wp_ajax_changesubcategorydropdown_action', 'changesubcategorydropdown');
add_action('wp_ajax_nopriv_changesubcategorydropdown_action', 'changesubcategorydropdown');
function changesubcategorydropdown(){
if(!isset($_POST['maincategory']))
return;
if( isset($_POST['maincategory']) && $_POST['maincategory']<0 )
return;
header("Content-Type: application/json");
$main_category = $_POST['maincategory'];
global $wpdb;
$results = $wpdb->get_results(
"SELECT * FROM {$wpdb->prefix}expert_jobs_categories WHERE id_parent IN ($main_category) AND id_category NOT BETWEEN 123 AND 198 "
);
echo json_encode($results);
wp_die();
}
function wpb_hook_javascript() {
if (is_page ('covid-19-resources-for-artists')) {
?>
<script type="text/javascript">
// your javscript code goes here
function gotosite() {
window.location = document.getElementById("menu").value; // JQuery: $("#menu").val();
}// your javscript code goes here
</script>
<?php
}
}
add_action('wp_head', 'wpb_hook_javascript');
Have also tried adding it like this outside of the main php tags -
<?php
add_action('wp_head', 'wpb_add_googleanalytics');
function wpb_add_googleanalytics() { ?>
// Paste your Google Analytics code from Step 4 here
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx');
</script>
<?php } ?>
can someone please tell me what I am doing wrong? Since I cant place this in a comment here is additional commentary based on the very kind assistance provided so far:
=================================================================================
Thanks but sorry I dont understand. If I literally copy and paste what you have written then it breaks the page. If I compare the first lines of your code to the top of the lines of code that exists then they too are different.
<?php
function denRegScript() {
wp_enqueue_style(
'datatable-css',
get_theme_file_uri('/assets/css/datatables.css'),
false
);
wp_enqueue_script(
'datatables-js',
get_theme_file_uri('/assets/js/datatables.min.js'),
array('jquery'),
'0.1',
true
);
}
add_action('wp_enqueue_scripts','denRegScript');
// …
compared to current lines-27:
<?php
function denRegScript(){
wp_enqueue_style( 'datatable-css', get_theme_file_uri('/assets/css/datatables.css'), false );
wp_enqueue_script( 'datatables-js', get_theme_file_uri('/assets/js/datatables.min.js'), array('jquery'), '0.1', true );
}
add_action('wp_enqueue_scripts','denRegScript');
add_action('wp_ajax_movie_datatables', 'datatables_server_side_callback');
add_action('wp_ajax_nopriv_movie_datatables', 'datatables_server_side_callback');
//add_action('wp_ajax_nopriv_movie_datatables', 'datatables_server_side_callback');
function datatables_server_side_callback()
{
header("Content-Type: application/json");
$request = $_GET;
$json_pre_result = newfunc($request);
if(isset($json_pre_result)) {
echo json_encode($json_pre_result);
}
wp_die();
If I just copy this portion:
function wpb_hook_javascript() {
if ( is_page( 'covid-19-resources-for-artists' ) ) {
?>
<script type="text/javascript">
function gotosite() {
window.location = document.getElementById("menu").value; // JQuery: $("#menu").val();
}
</script>
<?php
}
}
add_action( 'wp_head', 'wpb_hook_javascript' );
function wpb_add_googleanalytics() {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx');
</script>
<?php
}
add_action( 'wp_head', 'wpb_add_googleanalytics' );
and place it in the last portion of the code then it does nothing when I code inspect and check the head, there is no analytics code called. What am I missing?
Upvotes: 0
Views: 476
Reputation: 1053
Your PHP opening and closing tags are incorrect.
They should look like this:
<?php
add_action( 'wp_head', 'wpb_add_googleanalytics' );
function wpb_add_googleanalytics() {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx');
</script>
<?php
}
function denRegScript() {
// …
Edit: You could place your Google Analytics code at the end of functions.php
, and your PHP opening/closing tags would look like this:
<?php
function denRegScript() {
wp_enqueue_style(
'datatable-css',
get_theme_file_uri('/assets/css/datatables.css'),
false
);
wp_enqueue_script(
'datatables-js',
get_theme_file_uri('/assets/js/datatables.min.js'),
array('jquery'),
'0.1',
true
);
}
add_action('wp_enqueue_scripts','denRegScript');
// …
function wpb_hook_javascript() {
if ( is_page( 'covid-19-resources-for-artists' ) ) {
?>
<script type="text/javascript">
function gotosite() {
window.location = document.getElementById("menu").value; // JQuery: $("#menu").val();
}
</script>
<?php
}
}
add_action( 'wp_head', 'wpb_hook_javascript' );
function wpb_add_googleanalytics() {
?>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxx');
</script>
<?php
}
add_action( 'wp_head', 'wpb_add_googleanalytics' );
Upvotes: 1