0

I want to grab data from my theme options and have to use them in meta option fields in custom post types. I know how to grab theme option data and show them on front pages but don't know how to grab it for backend usage. I want to use data from one of my theme options text field and put it into my metabox text field.

Actually I have two check boxes on my metabox text field and also two metabox text fields above that text field. Also two text fields in my theme options in which I have to put some html and javascript code. So here is my code:

Metabox text fields and checkboxes:

<input type="textarea" id="c" value="Your Name" />
<input type="textarea" id="d" value="My Name" />
<input type="checkbox" id="a" />
<input type="checkbox" id="b" />
<input type="textarea" id="e" />

Theme Option Text fields:

<input type="textarea" id="f"  />
<input type="textarea" id="g"  />

I have to put javascript in theme option text fields somewhat like this:

<div>
<script type=text/javascript> name: 'My Name is/', name2: 'Your name is/', </script> 
</div>

Now comes the real worry. I want that when I click check box with the id 'a', the code from the theme options text field with the id 'f' will be put into the metabox text field with the id 'e' but with little modification. The modification i need is that the data from metabox text fields with the id 'c' and 'd' will first be added to the code that has been grabbed from the theme option text field with the id 'f' in such a way that value of metabox text field with the id 'c' is added to the "name: My Name is/(Here will be the value of text field with the id 'c')" and value of metabox text field with the id 'd' is added to the "name2: Your Name is/(Here will be the value of text field with the id 'd')".

I am also using jquery code for these checkboxes behaviour. Here is my jQuery code.

$(function () {
$('#a, #b').change(function () {
    var $a = $('#a'), $b = $('#b'), $c = $('#c');
    if (this.id == 'a' && this.checked) {
       $c.val('Hello World!');
       $b.prop('checked', false);
    } else if (this.id == 'b' && this.checked) {
       $c.val('Not hello World!'); 
       $a.prop('checked', false);
    } else {
       $c.val('');
    }
});
});

​ Obviously there is flaw in this jQuery code as I don't want these values such as Hello world or Not Hello World of my metabox text field with the id 'c'. I want the value of that field as I explained earlier. Please help me in this regard. I am very much frustrated.

4
  • You grab the data the same as you do for the Front End
    – Bainternet
    Commented Oct 3, 2012 at 18:36
  • Your question is very vague. You grab the options data in the exact same way on both front and back-ends. Are you trying to ask how to create a metabox? Commented Oct 3, 2012 at 18:37
  • Well please let me clarify it a bit. I want to use data from one of my theme options text field and put it into my metabox text field. Don't know how to do it. Please help me. Thanks Commented Oct 3, 2012 at 19:50
  • Do you want to put the values of the theme option to every post that you create or is it only for a few ?
    – bagpipper
    Commented Aug 9, 2016 at 10:23

2 Answers 2

1

You can use the get_option function on the back-end too. There are several possibilities on how to pass your option value to your script:

  1. Use wp_localize_script function to pass PHP values to your script (see WordPress Codex page for this function).
  2. Print your option value along with name and name2 values inside a script tag.
  3. Use AJAX. When user clicks your checkbox A, you send an AJAX request to the server requesting the value of your option.

The rest is just a matter of joining all the pieces together and putting them inside your textfield using jQuery.

0

Hope this help.

Use WPAlchemy to set up your metaboxes. http://www.farinspace.com/wpalchemy-metabox/. So far it is the easiest way to do it, IMHO. Afteryou setup your <input type="text"> field just add <?php echo get_option('your option name')?> with a conditional to check if it already has a value.

Untested code :

<?php
    if($metabox->get_the_value('_coordinate')){
        $value = $metabox->get_the_value('_coordinate');
    } else {
        $value = get_option('your option name');
    }
?>
<input type="text" name="<?php $metabox->the_name('_coordinate'); ?>" value="<?php echo $value; ?>"/>
1
  • Thanks for your response. Please check the updated question as I have edited it to clarify a bit what I really needed. Please answer it. Commented Oct 4, 2012 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.