• Resolved Josh

    (@josh401)


    Hi,

    I am using this function to store an array for selecting a color scheme.

    function jwl_tinycolor_css_callback_function() {
    	$options = get_option('jwl_tinycolor_css_field_id');
    	$items = array("Default", "Pink", "Green");
    	echo "<select id='tinycolor' name='jwl_tinycolor_css_field_id[tinycolor]'>";
    	foreach($items as $item) {
    		$selected = ($options['tinycolor']==$item) ? 'selected="selected"' : '';
    		echo "<option value='$item' $selected>$item</option>";
    	}
    	echo "</select>";
     }

    The problem I am having is pulling each value from the array and assigning it to a variable… so that I can run it against an if/else statement.

    I can echo out the get_option('jwl_tinycolor_css_field_id'), but it only results in the word Array.

    Thank you very much for any help.

Viewing 14 replies - 16 through 29 (of 29 total)
  • Marventus

    (@marventus)

    Hi Josh!

    Ok, I think I now understand better what you are trying to do. There are 2 potential reasons that could account for what’s happening to you:
    1. As you mentioned in your last post, the JSON encoding function could not be saving the user-defined array properly;
    2. WP might be escaping or otherwise tampering with the user-defined array entered in the input field with one of the many filters it uses to sanitize, escape, and/or process user input data.

    In order to test these hypothesis, I would recommend to print_r both sets of arrays:
    1. Array before JSON encoding with array after JSON encoding, to check if the encoded data matches the array entered by the user;
    2. User-defined array in input field with a print_r of get_option('jwl_userstyles_css_field_id').

    Let me know if you find any differences and we’ll take it from there.

    Good luck!

    Thread Starter Josh

    (@josh401)

    Ahhh… Okay. Now I’m beginning to see what’s going on.

    Here is the array I’m using which works when entered directly into my plugins main.php file:

    array(
        	array(
        		'title' => 'Button',
        		'selector' => 'a',
        		'classes' => 'button'
        	),
            array(
            	'title' => 'Callout Box',
            	'block' => 'div',
            	'classes' => 'callout',
            	'wrapper' => true
            ),
            array(
            	'title' => 'Bold Red Text',
            	'inline' => 'span',
            	'styles' => array(
            		'color' => '#f00',
            		'fontWeight' => 'bold'
            	)
            )
        )

    And here is the output using print_r BEFORE the json encoding (which is using the $style_formats_temp variable from above):

    Array ( [0] => Array ( [title] => Button [selector] => a [classes] => button ) [1] => Array ( [title] => Callout Box [block] => div [classes] => callout [wrapper] => 1 ) [2] => Array ( [title] => Bold Red Text [inline] => span [styles] => Array ( [color] => #f00 [fontWeight] => bold ) ) )

    And here is the output using print_r AFTER the json encoding (which is using the $settings_temp variable from above):

    array( array( 'title' => 'Button', 'selector' => 'a', 'classes' => 'button' ), array( 'title' => 'Callout Box', 'block' => 'div', 'classes' => 'callout', 'wrapper' => true ), array( 'title' => 'Bold Red Text', 'inline' => 'span', 'styles' => array( 'color' => '#f00', 'fontWeight' => 'bold' ) ) )

    I might be editing this a little over the next few minutes ??

    I feel like it’s SOOOOO close…

    Thread Starter Josh

    (@josh401)

    Okay.. here is what’s happening after the JSON encoding…

    [Code moderated as per the Forum Rules. Please use the pastebin]

    There is a line where the // is causing the rest of the content to not be rendered… because it thinks it’s a developers note.

    This line, specifically;

    [wp_fullscreen_content_css] => https://www.joshlobe.com/wp-includes/js/tinymce/plugins/wpfullscreen/css/wp-fullscreen.css

    Everything after that // after the http: is being rendered as a note.

    I’ll need to play with this some more now that I kind of see what’s going on. It’s hard to explain, when I don’t quite understand it myself ??

    I’ll post back shortly with some more refined results… and if you think of anything along the way.. please share ??

    Thread Starter Josh

    (@josh401)

    Funny… I can’t ever seem to get a response on any of my queries… yet my code is moderated and snipped without hesitation ??

    My apologies to the mods for not using pastebin.

    Anyways.. here is the pastebin link for the code above:
    https://pastebin.com/b23QW0Ub

    Marventus

    (@marventus)

    Indeed we are close.
    Your code in the last post got moderated because there is a limit of 10 lines of code. If your code is longer than that, you are supposed to use Pastebin.
    Could you create a Pastebin of that code and paste the link here so I can see the whole thing?
    Based on your last post, it seems you are trying to use the DB value without json_decoding it first, although shouldn’t get_option be taking care of that?
    Scratching my head again.

    Thread Starter Josh

    (@josh401)

    Here is the function I used to print out the code that was pasted in pastebin:

    function my_mce_before_init( $settings_temp ) {
    		$style_formats_temp = get_option('jwl_userstyles_css_field_id');
        	$settings_temp['style_formats'] = json_encode( $style_formats_temp );
    		print_r($settings_temp);
        	return $settings_temp;
    	}
    	add_filter( 'tiny_mce_before_init', 'my_mce_before_init' );

    Marventus

    (@marventus)

    So, going back to your function. Does this work?

    function my_mce_before_init( $settings_temp ) {
    		$style_formats_temp = json_decode(get_option('jwl_userstyles_css_field_id'), true);
    		return $style_formats_temp;
        	$settings_temp['style_formats'] = json_encode( $style_formats_temp );
        	return $settings_temp;
    	}
    	add_filter( 'tiny_mce_before_init', 'my_mce_before_init' );

    This way you would force the get_option helper to return an array, in case it “insists on” retrieving an object class, thus breaking the array contained under that option name.

    Thread Starter Josh

    (@josh401)

    Okay… that code doesn’t break the editor anymore… however the styles dropdown now contains 4 {#undefined} styles in the dropdown menu of the editor.

    Unfortunately, I have to go to school now. I’ll be gone about four hours.

    This is going to be the first thing I get into when I return home. You have given me a few new ideas I probably need to experiment with.

    I’m also going to send you a .zip file of my plugin to the email address you provided above. This file will have all my current work, so it’s not complete yet. If you look in the main.php file, at the very bottom, you will see the code I am working with.

    If you activate the plugin, you can go to the settings page in the admin panel and see what I’m trying to accomplish.

    Thanks again… and I’ll shoot you an email now.

    Thread Starter Josh

    (@josh401)

    Okay… using your function above; if I add print_r() into the code like this:

    function my_mce_before_init( $settings_temp ) {
    		$style_formats_temp = json_decode(get_option('jwl_userstyles_css_field_id'), true);
        	$settings_temp['style_formats'] = json_encode( $style_formats_temp );
    		print_r($settings_temp['style_formats']);
        	return $settings_temp;
    	}
    	add_filter( 'tiny_mce_before_init', 'my_mce_before_init' );

    The output of the print_r() is null.

    Thread Starter Josh

    (@josh401)

    Okay… I’ve made a little progress.

    I’ve changed my way of doing things though…

    First, I changed my function to this:

    function user_styles_dropdown($user_styles) {
    		$user_styles['style_formats'] = get_option('jwl_userstyles_css_field_id');
    		return $user_styles;
    	}
    	add_filter( 'tiny_mce_before_init', 'user_styles_dropdown' );

    Then, in the user box to enter custom styles, I decided to try it with the JSON encoding rather than using arrays.

    Here is the code in the admin panel for jwl_userstyles_css_field_id:

    [{"title":"Button","selector":"a","classes":"button"},{"title":"Callout Box","block":"div","classes":"callout","wrapper":true},{"title":"Bold Red Text","inline":"span","styles":{"color":"#f00","fontWeight":"bold"}}]

    Now, everything works perfectly.

    DRAWBACKS…
    1. JSON is more difficult to write in than PHP arrays
    2. A feature like this might be intimidating to novice users
    3. I’m really going to have to document my help section with examples, definitions, etc.

    ADVANTAGES…
    1. IT WORKS!!

    So… I don’t know?? What do you think?? Is this overkill, or a breakthrough?? lol

    Marventus

    (@marventus)

    Sorry I couldn’t get back to you sooner: I was wrapping up something else.
    Your solution works, of course, but I, for one, would love to understand why the other way isn’t working.
    Also, I think it might be a little overkill to have to type in JSON code in the input field, although there are a lot of converters out there that do the trick for you in a heartbeat.
    If you are not working under a deadline or are in any sort of rush, I’d like to keep thinking about this a little longer and run some additional tests.
    I’ll let you know for yey or ney as soon as I’m done.
    Have a good night!

    Thread Starter Josh

    (@josh401)

    I agree with everything you said. I too would like to understand why the other way was not working.

    No, I’m in no hurry whatsoever. I’ll be releasing a new version of the plugin in the repository soon, however this feature isn’t quite ready for that release.

    Thank you so much Marvelous Marventus!

    I’ll keep an eye on this thread… curious as to your findings ??

    Marventus

    (@marventus)

    Hi Josh,

    Ok, so I have some good news and some bad news:

    The good news is that I know what the problem is and why the user styles don’t work with normal arrays but do with JSON format: the issue is that the original array is not being converted by WP to JSON before storing it in the DB. Indeed, I checked my DB and the value stored under jwl_userstyles_css_field_id is a mirror of the array in the settings page:

    array(array('title'=>'Button','selector'=>'a','classes'=>'button'),array('title'=>'CalloutBox','block'=>'div','classes'=>'callout','wrapper'=>true),array('title'=>'BoldRedText','inline'=>'span','styles'=>array('color'=>'#f00','fontWeight'=>'bold')))

    That explains why get_option does not work either, since it expects a JSON encoded value to decode.

    The bad news is that I was not able to solve the problem. I tried everything and no cigar!
    1. Adding a hook to josh_mce_before_init and then hooking the problem function to it (instead of to tiny_mce_before_init , which is what we were trying to do originally;
    2. Merging josh_mce_before_init and tiny_mce_before_init with a conditional statement to check for the value of jwl_userstyles_css_field_id, using either array_push or array_merge to insert the user-defined style array inside the $style_formats array. With array_push I got it not to break the editor, but the new style was shown as {#undefined}.
    3. Adding a sanitize_callback parameter to:

    register_setting('jwl_options_group','jwl_userstyles_css_field_id' );

    so as to force the JSON format at that point.

    To summarize, I see two possible explanations for this:
    I. WP is sanitizing the user input and breaking the array entered inside the textarea before it is stored in the DB;
    II. We are missing a step somewhere so as to JSON encode the input before storing it in the DB.

    I’m completely out of ideas for the moment, but perhaps you might of think of something after reading my post.

    Going to sleep now!

    P.S.: Your plugin is really great. Kudoz!

    Marventus

    (@marventus)

    Hi Josh,

    I made a lot of progress on this this evening. I’ll send you a detailed email tomorrow morning cuz right now I’m exhausted.
    Cheers!

Viewing 14 replies - 16 through 29 (of 29 total)
  • The topic ‘How do I get variables from this function?’ is closed to new replies.