How do I get variables from this function?
-
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 wordArray
.Thank you very much for any help.
-
Hi again, ??
I don’t know what the exact structure of your
jwl_tinycolor_css_field_id
option value is, but by the looks of it, it seems that you are trying to compare an integer value (an ID) with a string value (i.e., the name of your colors), so that if statement will always return false.
If you also have the color names stored in your options, try matching each $item to that.Cheers!
Yeah, I’ve tried
$item
to no avail.Here is the register settings code:
add_settings_field('jwl_tinycolor_css_field_id', __('Change the color of the Editor.','jwl-ultimate-tinymce'), 'jwl_tinycolor_css_callback_function', 'ultimate-tinymce3', 'jwl_setting_section3'); register_setting('jwl_options_group','jwl_tinycolor_css_field_id');
Am I trying to hard on this? Is there a simpler way to create a dropdown on the admin side, and then use each option in the functions page?
Edit: I just reread what I wrote and I realized it doesn’t make any frickin sense, hahaha. Let me look into this a little more. ??
lmao… you are THE MAN!!!
If I echo out the
get_option('jwl_tinycolor_css_field_id')
, it simply echos the wordArray
.Basically, I’ve set up two alternative colors for the tinymce editor. So, I need to tell my function if “Pink” is selected from the dropdown, display the pink stylesheet. If “Green” is selected, display the green stylesheet.
Here is what I’ve been working with… but it ALWAYS displays whatever stylesheet is listed first, regardless of the setting option in the admin panel.
if(($options['tinycolor']==$item) == "Pink") { function change_mce_colors_pink() { $urlpink = plugin_dir_url( __FILE__ ) . 'css/change_mce_pink.css'; // Color Options echo "<link rel='stylesheet' type='text/css' href='$urlpink' />\n"; // Added for admin panel css styles } add_action('admin_head', 'change_mce_colors_pink'); } elseif(($options['tinycolor']==$item) == "Green") { function change_mce_colors_green() { $urlgreen = plugin_dir_url( __FILE__ ) . 'css/change_mce_green.css'; // Color Options echo "<link rel='stylesheet' type='text/css' href='$urlgreen' />\n"; // Added for admin panel css styles } add_action('admin_head', 'change_mce_colors_green'); } else { echo ''; }
Thanks again… and please take your time.
Oh, and I’m not necessarily committed to the way I’ve coded it above. I’m learning as I go. Please feel free to criticize my code; or suggest a better alternative.
Ok, so I tried the following code on my local WP site:
<?php $options = array ('tinycolor' => 'Green'); $items = array("Default", "Pink", "Green"); echo "<select id='tinycolor' name='$options[tinycolor]'>"; foreach($items as $item) { $selected = ($options['tinycolor']==$item) ? 'selected="selected"' : ''; echo "<option value='$item' $selected>$item</option>"; } echo "</select>"; ?>
Basically, I matched the internal structure of the $options and $jwl_tinycolor_css_field_id arrays and it worked like a charm. Here’s the HTML output:
<select id='tinycolor' name='Green'> <option value='Default'>Default</option> <option value='Pink' >Pink</option> <option value='Green' selected="selected">Green</option> </select>
So after this accomplishment starts the severe head scratching trying to figure out why it does not work dynamically…
Hmmm, could you copy/paste here the output of the following code:<?php print_r($jwl_tinycolor_css_field_id}; ?>
I’d love to see the exact contents of that array. Thanks!
Ok, I think your last reply came in between one of my multiple Edits.
Your code seems fine but I think the problem is in your if statements.What I would do is retrieve the color option from the DB and make the stylesheet filename dynamic, like so:
?> <?php function change_mce_colors() { $current_option = strtolower($options['tinycolor']); $colorurl = plugin_dir_url( __FILE__ ) . 'css/change_mce_'.$current_option.'.css'; // Color Options wp_register_style('tiny_color_mce', $colorurl, '', 1.0, 'screen'); wp_enqueue_style('tiny_color_mce'); } add_action('admin_head', 'change_mce_colors'); ?>
You can change the name of the handle (‘tiny_color_mce’) to whatever you like. Let me know if this works.
P.S.: Always try to use wp_enqueue_style() and wp_enqueue_script() for CSS stylesheets and JS scripts, ??
P.S.2: I almost forgot: Since you were using the $options array in another function, you might have to declare it as global or pass it to this other function.
Okay… I am still unsuccessful… but I do like the direction you have taken us ?? That second code is a LOT cleaner!!
Well, when I use the code above,
print_r($jwl_tinycolor_css_field_id};
, I get nothing. I mean, it doesn’t display anything. No errors.. no warnings.. no text at all.I’m still a noob, and I’m not sure what you mean by the globals. I added
global $options;
, but nothing.Does this have anything to do with serializing and un-serializing an array? The content in the database has a weird sort of code to it.
EDIT: Here is how the info is being stored in the database:
a:1:{s:9:"tinycolor";s:4:"Pink";}
OH MY GOSH!! It’s working!!!
I had to add:
$options2 = get_option('jwl_tinycolor_css_field_id');
to the code you provided in your last post. And than I changed the
$options
tooptions2
in the string to lower function you provided.So, the final code is:
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>"; } function change_mce_colors() { $options2 = get_option('jwl_tinycolor_css_field_id'); $current_option = strtolower($options2['tinycolor']); $colorurl = plugin_dir_url( __FILE__ ) . 'css/change_mce_'.$current_option.'.css'; // Color Options wp_register_style('tiny_color_mce', $colorurl, '', 1.0, 'screen'); wp_enqueue_style('tiny_color_mce'); } add_action('admin_head', 'change_mce_colors');
and works beautifully!!!!!
I cannot thank you enough.
If I were to want to go see what your paypal page looks like… how would I go about doing that?? ??
Hi Josh,
I’m really glad it’s working out as expected!
About the $option2 variable, since it’s value is the same as $options, you can skip that step by declaring $options global in thejwl_tinycolor_css_callback_function
function, like so:$options = get_option('jwl_tinycolor_css_field_id'); global $options;
Then you will be able to use that variable in your
change_mce_colors
function without redefining it.About my PayPal account, you really don’t have to pay me for this: I love doing this stuff and helping people out as much as I can.
Do let me know if there’s anything else I can help you with.
Cheers!
Edit: Since we are using wp_enqueue_style for the second function, it would make more sense to hook that function to
wp_print_styles
instead ofwp_head
:add_action('wp_print_styles','change_mce_colors');
Okay, changing to the
wp_print_styles
did nothing.it would make more sense to hook that function to wp_print_styles instead of wp_head:
I’m actually hooking to
admin_head
and notwp_head
. Not sure if that makes a difference.I agree. I reference a few different style sheets throughout my plugin code… and I need to change them to the enqueue styles.
Okay. Can I pick your brain on another?
I have my options page in the plugin admin settings area. I would like to get the value from the
add_option
setting, and use it in an array on my functions page.So, something like this (which I know doesn’t work).
$style_user_array = array ( get_option('jwl_userstyles_css_field_id') );
Actually, here is the entire code snippet:
function my_mce_before_init( $settings5 ) { $style_user_array = array ( get_option('jwl_userstyles_css_field_id') ); $settings5['style_formats'] = json_encode( $style_formats ); return $settings5; }
The user will add the array in the
get_option('jwl_userstyles_css_field_id') );
area, and then I need to pull it in for json encoding. Should I use an echo or return to “print” the code from the user in the function?About my PayPal account, you really don’t
Humor me. I know I’m fairly good at what I do. So, if you are helping me… then you have put some serious effort into the studying, exploring, and developing of your techniques… which should be rewarded by people like me… even if it’s modest. It takes a HELL of a lot of time, patience, and frustration to learn what we know ??
Hi Josh!
I’m actually hooking to admin_head and not wp_head. Not sure if that makes a difference.
Actually, it does make a difference. Sorry I didn’t pick up on that before. The hook I mentioned before,
wp_enqueue_style
, is for the frontend. You sould then hook your second function toadmin_enqueue_style
, which will only affect the backend.
As for yourmy_mce_before_init
function, if you want to transform the value of yourjwl_userstyles_css_field_id
option name into an array and to know the name of the key the value will be available at, you should do something like this:$style_user_array = array ('somekey' => get_option('jwl_userstyles_css_field_id');
Then, to use that array and retrieve the value under
somekey
, you would go:$style_user_array[somekey];
What I don’t see in the code you posted above is a link between this array and your $settings5 variable, but that’s probably because it is a partial copy/paste.
Let me know if this answers your question, and if it doesn’t, I’ll give it another try, ??
Humor me. I know I’m fairly good at what I do. So, if you are helping me… then you have put some serious effort into the studying, exploring, and developing of your techniques… which should be rewarded by people like me…
You make an excelent point sir. I shall humor thee then, ?? However, I would rather not paste that kind of info in here. If you send me an e-mail to wp-forums [at] elblawg [dot] com, I’ll send you the actual email I use for my PayPal.
Okay. I really need to do some more research on the enqueue stuff. I still can’t get it with the
admin_enqueue_style
, but I’m sure it’s because I don’t know what I’m doing with that. Let me “learn” it better, and I’ll come back if I’m still stuck.Okay, this function works fine:
function my_mce_before_init( $settings_temp ) { $style_formats_temp = 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' )) ); $settings_temp['style_formats'] = json_encode( $style_formats_temp ); return $settings_temp; } add_filter( 'tiny_mce_before_init', 'my_mce_before_init' );
It takes those three titles, adds them to the dropdown in tinymce, and everything works nicely. But, I have to define them like above.
What I’m trying to do is allow the user to be able to build their own array from the admin panel, and pull it into this function.
So, I’m taking all the:
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 added it into my options box in the admin panel which is associated with the
jwl_userstyles_css_field_id
option.However, when I run this code:
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 ); return $settings_temp; } add_filter( 'tiny_mce_before_init', 'my_mce_before_init' );
It breaks.
Hi Josh,
I read your post carefully, but there is something I don’t fully see or understand. What exactly do you mean by:
What I’m trying to do is allow the user to be able to build their own array from the admin panel, and pull it into this function.
Do you mean allow the user to save and/or update options, or actually create new ones?
Because in the first case scenario, you would use update_option.
Maybe I’m just missing information about how your plugin works and what it is you are trying to accomplish.Hi Josh,
Were you able to figure this out? Did my question make sense? Let me know if I’m getting it right.
Cheers!
Hello, my friend. I was sidetracked for a few days.
Actually, I have not resolved this yet. I think my issue lies with using the JSON encoding necessary for the tinymce. I can get it working within my plugin
main.php
file. But once I try to assign the exact same string to a variable, it doesn’t work.I’m going to attempt to explain this ??
1. Of course, I am working from within a plugin.
2. There is a text box in the admin panel of the plugin where a user can enter their own custom array.Example
array('title' => 'Bold Red Text','classes' => 'button')
This allows them to build their own styles dropdown list to use within the wordpress tinymce editor (probably only for intermediate users at this point, until I can include many examples).
Then, I pull this “string” from the admin page into the function used to code all the styles.
So, this will work beautifully…
function my_mce_before_init( $settings_temp ) { $style_formats_temp = array('title' => 'Bold Red Text','classes' => 'button'); 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' );
However, this will not…
function my_mce_before_init( $settings_temp ) { $style_formats_temp = get_option('jwl_userstyles_css_field_id'); 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' );
Where
jwl_userstyles_css_field_id
is the text box from the admin panel where the user can enter their custom string.Why would it work if I hardcode it? But not work when I try to pull it in from the admin panel via
get_option
?
- The topic ‘How do I get variables from this function?’ is closed to new replies.