0

I'm a beginner self-made in coding. I've got a complexe theme which works well in since 2012. Since then some stuff didn't work anymore. Though I often use to change and correct what's needed to be changed or is deprecated. Tired of always changing stuff I've updated wordpress to adapt that theme from now.

I've got that new warning :

jQuery.parseJSON requires a valid JSON string

concerning that exact line :

hooks = jQuery.parseJSON(hooks);

in this part of code :

/**
 * SMOF js
 *
 * contains the core functionalities to be used
 * inside SMOF
 */

jQuery.noConflict();

/** Fire up jQuery - let's dance! 
 */
jQuery(document).ready(function($){

        jQuery("select.seo-select").children().removeAttr('selected');
        jQuery("select.seo-select").change(function(){
                $input = jQuery(this).parent().prev();
                $text = jQuery(this).val();
                if($input.val() != ''){
                    $text = ' | '+jQuery(this).val();
                }
                $input.val($input.val()+$text);
                jQuery(this).children().removeAttr('selected');
        });

    //(un)fold options in a checkbox-group
    jQuery('.fld').change(function() {
        var $fold='.f_'+this.id;
        $($fold).slideToggle('normal', "swing");
    });

    //delays until AjaxUpload is finished loading
    //fixes bug in Safari and Mac Chrome
    if (typeof AjaxUpload != 'function') { 
            return ++counter < 6 && window.setTimeout(init, counter * 500);
    }

    //hides warning if js is enabled            
    $('#js-warning').hide();

    //Tabify Options            
    $('.group').hide();

    // Display last current tab 
    if ($.cookie("of_current_opt") === null) {
        $('.theme_options .group:first').fadeIn('fast');    
        $('.theme_options #of-nav li:first').addClass('current');
    } else {

        var hooks = $('#hooks').html();
        hooks = jQuery.parseJSON(hooks);
        if(hooks) {
                    $.each(hooks, function(key, value) {

                            if ($.cookie("of_current_opt") == '#of-option-'+ value) {
                                    $('.theme_options .group#of-option-' + value).fadeIn();
                                    $('.theme_options #of-nav li.' + value).addClass('current');
                            }

                    });
                }

    }

I haven't found response for that problem in other posts. Except one that seemed to be relevant. I've tried to add this at the top as adviced in https://stackoverflow.com/a/24512828/8844128 but it didn't change anything :

var json = $.parseJSON(jsonString || "null");

Maybe it's a different problem. Does anybody has an explanation and even better a valid solution I could use?

  • jquery-migrate.js?ver=1.4.1:45
  • jquery.js?ver=1.12.4:2
  • wordpress : 4.9.4
  • site : My Site

As asked in the comments it's used in the theme's admin option interface pages when you trigger the background image popup. Here are the admin code where hook appears at line 159 :

jQuery.noConflict();
jQuery(document).ready(function($){

    //hide hidden section on page load.
    jQuery('#section-body_bg, #section-body_bg_custom, #section-body_bg_properties').hide();

    //delays until AjaxUpload is finished loading
    //fixes bug in Safari and Mac Chrome
    if (typeof AjaxUpload != 'function') { 
        return ++counter < 6 && window.setTimeout(init, counter * 500);
    }
    //hides warning if js is enabled            
    $('#js-warning').hide();

    //Tabify Options            
    $('.group').hide();

    // Display last current tab 
    if ($.cookie("of_current_opt") === null) {
        $('.group:first').fadeIn(); 
        $('#of-nav li:first').addClass('current');
    } else {

            var hooks = <?php
$hooks = of_get_header_classes_array();
echo json_encode($hooks);
?>;

                $.each(hooks, function(key, value) { 

                    if ($.cookie("of_current_opt") == '#of-option-'+ value) {
                        $('.group#of-option-' + value).fadeIn();
                        $('#of-nav li.' + value).addClass('current');
                    }

                });

            }

and then at line 1190 :

$header_class = ereg_replace("[^A-Za-z0-9]", "", strtolower($value['name']));
                    $jquery_click_hook = ereg_replace("[^A-Za-z0-9]", "", strtolower($value['name']));
                    $jquery_click_hook = "of-option-" . $jquery_click_hook;
                    $menu .= '<li class="' . $header_class . '"><a title="' . $value['name'] . '" href="#' . $jquery_click_hook . '">' . $value['name'] . '</a></li>';
                    $output .= '<div class="group" id="' . $jquery_click_hook . '"><h2>' . $value['name'] . '</h2>' . "\n";
                    break;

Is there a way I can send the file inline if it helps see how everything connects?

Jane Seneor
  • 11
  • 1
  • 5
  • Does `$('#hooks').html()` contain valid JSON? Edit - looked at your site, I don't see an element with id='hooks' on that page. – James Feb 10 '18 at 18:24
  • As the commend above mentioned, could you post a sample of your JSON string? In addition, have you tried $('#hooks').text()? Just in case there's some dodgy HTML in #hooks – gonzie Feb 10 '18 at 18:27
  • ok. I'm checking that... – Jane Seneor Feb 10 '18 at 20:04
  • it's in the admin part in the header as it is triggered when i open a media field to upload a background image. I'm updating the post with those chunk of code for you – Jane Seneor Feb 10 '18 at 21:03
  • @gonzie I'm sorry I don't understand where and how to try $('#hooks').text(). Can you help me how I should proceed? – Jane Seneor Feb 10 '18 at 21:22
  • maybe it's related to the fact that before jQuery 1.9.0, the $.parseJSON() method allowed some invalid JSON strings and returned null as a result without throwing an error, as it did work well before : [https://stackoverflow.com/a/24512828/8844128](https://stackoverflow.com/a/24512828/8844128) can anyone help me fix it? – Jane Seneor Feb 16 '18 at 00:29
  • @James I've added the admin-interface.php extract were hooks it called at the end of my post question. – Jane Seneor Feb 16 '18 at 00:46
  • It’s hard to see what’s going on here. In part 1 you have the variable hooks being constructed out of some json which is in the html element with id hooks, which still doesnt exist (so you get an error). Then in part 2 it is constructed out of a call to the php function of_get_header_classes_array. It seems that the technique in part 2 is reasonable. But there is no way that the php from part2 generates the javascript seen in part1. So what is going on there? – James Feb 16 '18 at 14:50
  • @James i've found that part : ' ' in my admin theme option page where I've got the message. Because it's in the admin part you can't see it. It toggles the options part. – Jane Seneor Feb 17 '18 at 15:25
  • Well that looks like it should work. If you definitely have the span id='hooks' and you definitely have on the same page the code that reads hooks from that span, it will work. [Here's an example](https://jsfiddle.net/o3t42qd4/2/) – James Feb 17 '18 at 17:44
  • I indeed have that span id='hooks' and the code for it but still JQuery Migrate give me that warning... I'm lost here – Jane Seneor Feb 20 '18 at 00:06

0 Answers0