Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I develop my first wordpress plugin. I have not really found a tutorial "easy to use", so I started it.

Currently, the "add_menu" did not add my link to manage settings. Yet when I activate my plugin, wordpress does not display an error and tells me "extension enabled"

Can you help me? Here is my code.

file "affiliations.php" (my-site/wp-content/plugins/afflilations)

/* 
Plugin Name: Importateur de flux d'affiliation
Plugin URI: http://my-site
Description: Plugin qui créé des articles en fonction de flux d'affiliations
Author: dev@my-site
Version: 1.0 
Author URI: http://my-site
*/  

/**
 * Need php 5
 */

if(5.0 > floatval(phpversion())) 
{
// Throw notice
add_action('admin_notices', 'affiliations_phpver_notice');
}
else
{
include_once('affiliations.class.php');
//create admin menu
add_action('admin_menu', array('affiliations.class', 'admin_menu'));
}

function affiliations_phpver_notice() 
{
echo "<div class=\"updated\"><p><strong>This plugin works with php5+.</strong></p></div>";
} 

file "affiliations.class.php" (my-site/wp-content/plugins/affiliations)

class Affiliations {

/**
 * Add options 
 * @return void
 */
public static function admin_menu() 
{
    add_options_page("Générer affiliations", 'Affiliations', 'manage_options', __FILE__, array('affliliations.class', 'plugin_options'));
}

/**
 * display html content options
 * @return void
 */
public static function plugin_options() 
{
    include('affiliations.tpl');    
}

}

file "affiliations.tpl" (my-site/wp-content/plugins/affiliations)

<strong>hello</strong>

Thank you for your support coming

share|improve this question
    
I found my error. The parameter affiliations.class is not good. The argument must be the name of the class : Affiliations ... –  Raphaël Oct 4 '11 at 10:22

2 Answers 2

I use this for my first theme please ignore little syntax error if any lol

update: All the following code should go into your functions.php file

Probably the best (and definitely the easiest) way to make a Theme Options Page is to use the Wordpress Settings API. One note... everywhere you see THEME_NAME in my code, just replace this with some unique phrase (I use my theme name).

First we need to tell WordPress that we are going to use some theme options. We do this by calling register_setting.

function add_theme_options_init()
{
register_setting( THEME_NAME . '_options_group', THEME_NAME . '_theme_options',  'validate_theme_options' );
}

Next create the actual options page where your options will be set.

function add_theme_options_page()
{

$page_title = 'Theme Options';
$menu_title = 'Theme Options';
$cap        = 'manage_options'; // capability required for access to this menu
$slug       = THEME_NAME . '-options';
$callback   = 'draw_theme_options_page';

add_options_page( $page_title, $menu_title, $cap, $slug, $callback );
}

Now add the 'draw_theme_options_page' function used as a callback in 'add_theme_options_page' that will output the HTML for your Theme Options page. In this example you can see that I add three fields: 1) A Facebook Page URL 2) A Twitter Handle 3) A field to add twitter search terms

function draw_theme_options_page()
{

?>

<div class="wrap">
<h2>Theme Options</h2>
<form method="post" action="options.php">

<?php

    // this should be the same as the second parameter of register_setting() 
    $opt_name = THEME_NAME . '_theme_options'; 

    // adds all the necessary hidden form fields
    settings_fields( THEME_NAME . '_options_group' );

    // get the existing options
    $options = get_option( $opt_name );

                // the options fields
    $opt = array(
        'fb_page' => $opt_name . '[fb_page]',
        'twitter' => $opt_name . '[twitter]',
        'twitter_search' => $opt_name . '[twitter_search]',
    ); 

?>

    <table class="form-table">

        <!-- Facebook Page URL -->
        <tr valign="top"><th scope="row">Facebook Page</th>
            <td><input type="text" name="<?php echo $opt['fb_page'] ?>" value="<?php echo $options['fb_page']; ?>" /></td>
        </tr>

        <!-- Twitter Stuff -->
        <tr valign="top"><th scope="row">Twitter ID</th>
            <td><input type="text" name="<?php echo $opt['twitter'] ?>" value="<?php echo $options['twitter']; ?>" /></td>
        </tr>

        <tr valign="top"><th scope="row">Twitter Search Terms</th>
            <td><input type="text" name="<?php echo $opt['twitter_search'] ?>" value="<?php echo $options['twitter_search']; ?>" /></td>
            <td class="description">Please separate search terms using a comma</td>
        </tr>

    </table>

    <p class="submit">
        <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
    </p>

</form>
</div>

<?php

}

Now we need to add our sanitizing function. This function just ensures that no badly formatted data is saved into our database.

function validate_theme_options( $input ) 
{   
$input['fb_page']        =  wp_filter_nohtml_kses( $input['fb_page'] );
$input['twitter']    =  wp_filter_nohtml_kses( $input['twitter'] );
$input['twitter_search'] =  wp_filter_nohtml_kses( $input['twitter_search'] );

return $input;
}

Now lastly we actually need to tie all these functions into the correct Wordpress Hooks so that our Theme Options page is added and works.

add_action( 'admin_init', 'add_theme_options_init' );
add_action( 'admin_menu', 'add_theme_options_page' );

This code is almost verbatim what I use in my own Theme, but you may need to play with it a little bit and check for typos.

share|improve this answer
    
That's interesting. It did not solve my problem, but it's a big help. thank you –  Raphaël Oct 5 '11 at 15:31
up vote 0 down vote accepted

Ok, Here is the solution.

In fact, in the args of add_action() or add_options_page(), I thought it was necessary to array("filename", "function") ... the good write is array("object", "method")

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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