0

https://codex.wordpress.org/index.php?title=Creating_Options_Pages&oldid=97268

This page shows how to create options pages.

        // theme-settings.php

        <?php
    add_action('admin_menu', 'talimiboard_register_menu_and_page');

    function talimiboard_register_menu_and_page()
    {
        add_theme_page('Talimiboard theme settings', 'Theme Settings', 'administrator', 'talimiboard_theme_settings', 'talimiboard_theme_settings');
        add_action('admin_init', 'talimiboard_register_theme_settings');
    }


    function talimiboard_register_theme_settings()
    {
        register_setting('talimiboard_theme_settings', 'talimi_mapapi');
        register_setting('talimiboard_theme_settings', 'talimi_smtphost');
        register_setting('talimiboard_theme_settings', 'talimi_smtpport');
        register_setting('talimiboard_theme_settings', 'talimi_smtpuser');
        register_setting('talimiboard_theme_settings', 'talimi_smtppass');
        register_setting('talimiboard_theme_settings', 'talimi_smtpfrom');
        register_setting('talimiboard_theme_settings', 'talimi_parallax');
    }


function talimiboard_theme_settings()
{ ?>

    <style>
        th {
            text-align: left;
        }

        .table-wrap {
            margin-top: 35px;
        }
    </style>

    <div class="wrap">
        <h2 style="text-align: center;">Theme Settings</h2>
        <form method="post" action="options.php">
            <?php settings_fields('talimiboard_theme_settings'); ?>
            <div class="table-wrap">
                <h3>Appearances</h3>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row"><label
                                    for="talimi_parallax">Homepage Parallax Effect</label></th>
                        <td><input type="checkbox" id="talimi_parallax" name="talimi_parallax"
                                   value="1" <?php echo checked('1', get_option('talimi_parallax')); ?>/></td>
                    </tr>
                </table>
            </div>

            <div class="table-wrap">
                <h3>API Keys</h3>
                <table>
                    <tr valign="top">
                        <th scope="row">Google Map API Key</th>
                        <td><input type="text" name="talimi_mapapi" value="<?php echo get_option('talimi_mapapi'); ?>"/>
                        </td>
                    </tr>
                </table>
            </div>

            <div class="table-wrap">
                <h3>SMTP Settings</h3>
                <table>
                    <tr valign="top">
                        <th scope="row">SMTP Host</th>
                        <td>
                            <input type="text" name="talimi_smtphost"
                                   value="<?php echo get_option('talimi_smtphost'); ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">SMTP Port</th>
                        <td>
                            <input type="text" name="talimi_smtpport"
                                   value="<?php echo get_option('talimi_smtpport', 587); ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <th>From</th>
                        <td>
                            <input type="text" name="talimi_smtpfrom"
                                   value="<?php echo get_option('talimi_smtpfrom'); ?>"/>
                        </td>
                    </tr>
                    <tr>
                        <th>SMTP Account</th>
                        <td><input type="text" name="talimi_smtpuser"
                                   value="<?php echo get_option('talimi_smtpuser'); ?>"/></td>
                    </tr>
                    <tr>
                        <th>Password</th>
                        <td><input type="password" name="talimi_smtppass"
                                   value="<?php echo get_option('talimi_smtppass'); ?>"/></td>
                    </tr>
                </table>
            </div>
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>"/>
            </p>
        </form>
    </div>
<?php } ?>

<?php 
// functions.php

require_once(get_template_directory_ur() . '/page-templates/theme-settings.php');

$google_map_api_key = get_option('talimi_mapapi');

 ?>

Then echo $google_map_api_key shows nothing..

closed as off-topic by Michael, Nathan Johnson, Christine Cooper, Dave Romsey, Jack Johansson Feb 28 '18 at 11:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?" – Michael, Nathan Johnson, Christine Cooper, Dave Romsey, Jack Johansson
If this question can be reworded to fit the rules in the help center, please edit the question.

  • can you please update the question and show the code you are using – DHL17 Feb 20 '18 at 10:36
  • I updated my question – Ibrahim Al Khalil Feb 20 '18 at 11:45
  • require_once(get_template_directory_ur() . '/page-templates/theme-settings.php'); may be this is a mistake u forget i in get_template_directory_uri() – DHL17 Feb 20 '18 at 12:36
  • Your code is working fine. on my local site. – DHL17 Feb 20 '18 at 12:45
0

I echoed out this $google_map_api_key = get_option('talimi_mapapi'); and its working fine.

try in functions.php add require_once get_template_directory() . '/page-templates/theme-settings.php';

or add wp-load.php in your file.

if you are using multi-site then try <?php get_site_option( $option, $default , $use_cache ) ?> reference https://codex.wordpress.org/Function_Reference/get_site_option

  • Please can you tell me at which file I have to add wp-load.php. functions.php or theme-settings.php?? – Ibrahim Al Khalil Feb 20 '18 at 14:31
  • I don't know why it didn't work before, now it working perfectly, Thanks for your response. – Ibrahim Al Khalil Feb 21 '18 at 2:19
  • add wp-load.php in theme-settings.php but its working so no need to add wp-load.php. – DHL17 Feb 21 '18 at 4:31
0

You use the get_option() function (see codex) passing in the name you gave your field using add_settings_field()

eg: echo get_option( 'eg_setting_name' );

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