Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a "universal" custom field in wordpress?

Tags:

wordpress

(wordpress version 3.4)

Ok, Here’s what I’m trying to do:

Create a page called “Custom Fields” that will never actually be seen by a user. It’s simply for the client to add in custom fields. He will constantly be needing to change these custom fields on a weekly/monthly basis.

I need these custom fields to be displayed inside the header, footer, or sidebars so that they will be universal on all pages.

Do you know the proper way to tweak the code so I can achieve this?

Thanks!

like image 526
Oneezy Avatar asked Jul 30 '12 04:07

Oneezy


2 Answers

Since Custom-Fields are meant on a per-post scope, I would recommend using the Options meta-data store instead. This way you can change the values from the Admin panel which is mush more convenient and consistent.

This is a tutorial showing how you can add this functionality to WP

//Custom Theme Settings
add_action('admin_menu', 'add_gcf_interface');

function add_gcf_interface() {
    add_options_page('Global Custom Fields', 'Global Custom Fields', '8', 'functions', 'editglobalcustomfields');
}

function editglobalcustomfields() {
    ?>
    <div class='wrap'>
    <h2>Global Custom Fields</h2>
    <form method="post" action="options.php">
    <?php wp_nonce_field('update-options') ?>

    <p><strong>My Name:</strong><br />
    <input type="text" name="myname" size="45" value="<?php echo get_option('myname'); ?>" /></p>

    <p><strong>Amazon ID:</strong><br />
    <input type="text" name="amazonid" size="45" value="<?php echo get_option('amazonid'); ?>" /></p>

    <p><strong>Today's Featured Website:</strong><br />
    <input type="text" name="todaysite" size="45" value="<?php echo get_option('todaysite'); ?>" /></p>

    <p><strong>Welcome Text:</strong><br />
    <textarea name="welcomemessage" cols="100%" rows="7"><?php echo get_option('welcomemessage'); ?></textarea></p>

    <p><input type="submit" name="Submit" value="Update Options" /></p>

    <input type="hidden" name="action" value="update" />
    <input type="hidden" name="page_options" value="myname,amazonid,todaysite,welcomemessage" />

    </form>
    </div>
    <?php
}
like image 164
Cleric Avatar answered Sep 20 '22 14:09

Cleric


With Cleric's code I got an error when saving the variable. It saved it allright but there was this error about code being deprecated.

Then I found this page Handling Plugins Options in WordPress 2.8 with register_setting() and it's now working perfectly in WP version 3.5.

like image 30
jazkat Avatar answered Sep 22 '22 14:09

jazkat