SlideShare une entreprise Scribd logo
Create a Settings Page For Your
WordPress Theme
Junejo Naeem Ahmed
WORDPRESS
WORDPRESS THEME
• Creating your own theme for WordPress is a great way to give
your blog or other WordPress powered web site an original
touch.
• But even the nicest looking theme is not that nice if you have
to get under the hood and edit the theme's HTML or PHP
code whenever it's time change some aspects of it.
• Especially, when it's not you but a paying customer using your
theme.
• Luckily, creating a settings page for your theme in WordPress
is not very hard!
DECIDING WHAT SETTINGS ARE NEEDED
• It all starts from the need: to create a clear and useful settings page,
you have to figure out the things that will need to be changed and
leave out everything else.
• Every new setting you add to the admin menus adds complexity to the
user interface and risks making the theme harder to use.
• That's why it's better to be careful and handpick the options that are
going to be changed often and leave out one time customizations that
can easily be done changing one file inside the theme.
• Another question to keep in mind is "Who is going to be changing
these settings?“
• If the user is familiar with PHP and WordPress, it might be reasonable
to expect that he is OK with embedding his Google Analytics code in
the code herself.
• but you shouldn't require that from a graphic designer, not to mention
a writer who doesn't even need to know anything about HTML and
CSS.
DECIDING WHAT SETTINGS ARE NEEDED
• Common ideas for things to define in theme settings include:
• The site's Google Analytics tracking code
• The number of sidebars and their positioning (left, right, maybe even
up and down)
• Page width
• The contents of your footer
• Options for features that are specific to the theme, such as custom
teaser formats.
DECIDING WHAT SETTINGS ARE NEEDED
• Once you have collected the list of theme features that you'd like to
control through a settings page, you are almost ready to start the
implementation.
• Before you move on and create your settings page, you can save time
by making sure that there isn't a WordPress feature already available
for the customization you have in mind.
• Widgets, custom menus, custom backgrounds and header images are
all useful tools for making your theme customizable with a lot less
work than required for creating your own settings.
HOOKING THE SETTINGS PAGE TO WORDPRESS
• Creating a settings page starts by creating a function that sets up the
menu and hooking it to the WordPress action admin_menu.
• This tells WordPress to call your function when its time to create the
menus so that everything is done at its proper time.
• Add this code to your theme's functions.php file:
• function setup_theme_admin_menus() {
• // We will write the function contents very soon.
• }
• // This tells WordPress to call the function named
"setup_theme_admin_menus"
• // when it's time to create the menu pages.
• add_action("admin_menu", "setup_theme_admin_menus");
HOOKING THE SETTINGS PAGE TO WORDPRESS
• We'll now put the code for creating the settings pages inside the
function we just created.
• When creating your settings page, you have the choice of either adding
the page as a submenu to one of the existing settings groups or of
creating your own top level menu.
• Adding a submenu is done with the function add_submenu_page:
• <?php add_submenu_page($parent_slug, $page_title,
$menu_title, $capability, $menu_slug, $function) ?>
HOOKING THE SETTINGS PAGE TO WORDPRESS
• $parent_slug is a unique identifier for the top menu page to which this
submenu is added as a child.
• $page_title is the title of the page to be added
• $menu_title is the title shown in the menu (often a shorter version of
$page_title
• $capability is the minimum capability required from a user in order to
have access to this menu.
• $menu_slug is a unique identifier for the menu being created
• $function is the name of a function that is called to handle (and
render) this menu page.
HOOKING THE SETTINGS PAGE TO WORDPRESS
• If you choose to add the menu page as a submenu to one of the
WordPress groups, you can use the following values as the
$parent_slug parameter:
• Dashboard: index.php
• Posts: edit.php
• Media: upload.php
• Links: link-manager.php
• Pages: edit.php?post_type=page
• Comments: edit-comments.php
• Appearance: themes.php
• Plugins: plugins.php
• Users: users.php
• Tools: tools.php
• Settings: options-general.php
HOOKING THE SETTINGS PAGE TO WORDPRESS
• The Appearance group looks like a good candidate for placing our
settings page.
• Let's try that, and create our first settings page.
• Here's an updated version of our menu setup function:
• function setup_theme_admin_menus() {
• add_submenu_page('themes.php',
• 'Front Page Elements', 'Front Page', 'manage_options',
• 'front-page-elements', 'theme_front_page_settings');
• }
• We still need to create the function theme_front_page_settings for
this to work. Here it is in its simplest form:
• function theme_front_page_settings() {
• echo "Hello, world!";
• }
HOOKING THE SETTINGS PAGE TO WORDPRESS
HOOKING THE SETTINGS PAGE TO WORDPRESS
• We also need to check that the user has the rights required for editing
the settings page.
• To do that, add the following code at the beginning of the settings
page function:
• // Check that the user is allowed to update options
• if (!current_user_can('manage_options')) {
• wp_die('You do not have sufficient permissions to access this page.');
• }
• Now, if a user who isn't allowed to manage options comes to the
settings page, he will see nothing but the message, "You do not have
sufficient permissions to access this page."
HOOKING THE SETTINGS PAGE TO WORDPRESS
• If your theme needs multiple settings pages, it can be confusing for the
user to look for them scattered all around the menu structure.
• In that case, creating your own settings group makes it easier for the
theme user to find all the menu pages for the theme.
• To add your own settings group, you need to create a top level menu
page and link the submenu pages to it.
• Here is a new version of our menu setup function.
• The add_menu_page function used to create the top level menu is
similar to add_submenu_page except that it doesn't take the
$parent_slug parameter.
HOOKING THE SETTINGS PAGE TO WORDPRESS
• function setup_theme_admin_menus() {
• add_menu_page('Theme settings', 'Example theme', 'manage_options',
• ‘my_theme_settings', 'theme_settings_page');
•
• add_submenu_page(‘my_theme_settings',
• 'Front Page Elements', 'Front Page', 'manage_options',
• 'front-page-elements', 'theme_front_page_settings');
• }
•
• // We also need to add the handler function for the top level menu
• function theme_settings_page() {
• echo "Settings page";
• }
HOOKING THE SETTINGS PAGE TO WORDPRESS
• If you test the code and refresh the WordPress admin, you'll see your
new menu group appear at the bottom of the menu list:
HOOKING THE SETTINGS PAGE TO WORDPRESS
• But something doesn't look quite right yet.
• Clicking the top menu element doesn't lead you to the "Front Page"
menu but a menu page called "Example theme.“
• This is not consistent with how the other WordPress menus function,
so let's do one more thing: by changing the $menu_slug attribute in
the add_submenu_page call to the same value as in the top level
menu, we can link the two menus so that selecting the top menu
selects the front page menu:
function setup_theme_admin_menus() {
add_menu_page('Theme settings', 'Example theme', 'manage_options',
‘my_theme_settings', 'theme_settings_page');
add_submenu_page(‘my_theme_settings',
'Front Page Elements', 'Front Page', 'manage_options',
‘my_theme_settings', 'theme_front_page_settings');
}
function theme_settings_page() {
}
HOOKING THE SETTINGS PAGE TO WORDPRESS
• Looks better. If you want to still improve the looks of your menu group,
there are two optional fields in the add_menu_page function that you
will find useful.
• Just add the values after the function name in the method call:
• $icon_url specifies the URL of an icon for the top level menu.
• $position specifies the position of your menu group in the menu list.
• The higher the value, the lower the position in the menu.
CREATING THE HTML FORM FOR THE SETTINGS PAGES
• Now that we have created the settings page, and it shows up
nicely in the side menu, it's time to start adding some
content.
• So, let's go back to the list of settings we had in mind, and
draft a page for editing them.
• we need a field for defining how many elements should be
listed on one row, and a list for defining the actual elements.
• To start from the easier, let's create a text field for the number
of elements on one row.
CREATING THE HTML FORM FOR THE SETTINGS PAGES
• Edit your settings page function:
• function theme_front_page_settings() {
• ?>
• <label for="num_elements">
• Number of elements on a row:
• </label>
•
• <input type="text" name="num_elements" />
• <?php
• }
CREATING THE HTML FORM FOR THE SETTINGS PAGES
• When you reload your settings page, you'll see the first settings field
appear:
CREATING THE HTML FORM FOR THE SETTINGS PAGES
• To make the settings page fit seamlessly in the WordPress experience and to give your
theme a professional touch, it's a best practice to use the CSS classes and styles that
WordPress uses in its own settings pages.
• A good way to learn the tricks is to just go ahead and analyze the WordPress source
code.
• The most important thing is to wrap your setting page with a div with the class "wrap".
• Within that div element, you can use many predefined styles such as headings, buttons,
and form fields.
• Let's start by styling the title of our settings page:
• We will create a h2 heading for the page (You can use the heading tags from h2 to h6 to
create headings with different sizes.)
• We will show the theme settings page icon before the heading. (You can use the
predefined WordPress icons with the screen_icon function.
• The function can take one of the following parameters: index, edit, upload, link-
manager, pages, comments, themes, plugins, users, tools or options-general.)
• We will put the input element inside a form and a table with the class form-table.
CREATING THE HTML FORM FOR THE SETTINGS PAGES
• function theme_front_page_settings() {
• ?>
• <div class="wrap">
• <?php screen_icon('themes'); ?> <h2>Front page elements</h2>
•
• <form method="POST" action="">
• <table class="form-table">
• <tr valign="top">
• <th scope="row">
• <label for="num_elements">
• Number of elements on a row:
• </label>
• </th>
• <td>
• <input type="text" name="num_elements" size="25" />
• </td>
• </tr>
• </table>
• </form>
• </div>
• <?php
• }
CREATING THE HTML FORM FOR THE SETTINGS PAGES
SAVING THE FORM
• The settings page looks good, but there is something missing: it doesn't do
anything yet.
• It's time to save some data.
• WordPress provides an easy system for saving theme and plugin settings
as key value pairs to the database using two functions:
• get_option and update_option.
• The data stored using the functions can be as simple as a number value or
as complex as an array nested multiple times.
• The handling of the form is done in the same function that renders the
form.
• To know whether a form was submitted or not, we add a hidden field,
update_settings to the form and then check whether that field was sent or
not in the handling function.
SAVING THE FORM
• if (isset($_POST["update_settings"])) {
• // Do the saving
• }
• The hidden field that goes inside the form looks like this:
• <input type="hidden" name="update_settings" value="Y" />
SAVING THE FORM
• Let's start by saving the easier setting, num_elements.
• We'll escape the attribute to make sure the user isn't sending malicious
content in the from of HTML tags and then save it to the WordPress
settings storage.
• When using update_option, we don't need to worry about whether the
setting has already been saved or not.
• $num_elements = esc_attr($_POST["num_elements"]);
• update_option("theme_name_num_elements", $num_elements);
SAVING THE FORM
• Before we move to saving the list, let's add the current value of
num_elements to the settings form so that the user always sees what
value he has entered in before deciding the next value.
• This will also help us test that the value was actually saved.
• <input type="text" name="num_elements" value="<?php echo
$num_elements;?>" size="25" />
• And for cases where we haven't saved anything yet, we'll need to load the
current value from options, so let's add this piece of code to be executed
when there is no form submitted.
• $num_elements = get_option("theme_name_num_elements");
SAVING THE FORM
• When a form is saved, it's important to notify the user so that he isn't left
wondering whether something happened or not.
• So, let's render a simple notice saying "Settings saved." right after the
update_option:
• <div id="message" class="updated">Settings saved</div>
USING THE SETTINGS INSIDE THE THEME
• Saving and showing settings values within the admin area is great, but
what really counts is how you use them to customize your theme, so now,
we have come to the point where it's time to take our settings and do
something cool with them.
• From here on, changes go to index.php instead of functions.php. First,
we'll read the options to variables:
• <?php
• $num_elements = get_option("theme_name_num_elements");
• ?>
CONCLUSION
• Now, we have created a settings page for a custom theme.
• The theme is far from complete, but I hope this introduction got you
started with adding settings and customizable elements to your next
WordPress theme.

Contenu connexe

Tendances

Webiny CMS Starter Guide
Webiny CMS Starter GuideWebiny CMS Starter Guide
Webiny CMS Starter Guide
Webiny
 
Easy Guide to WordPress Theme Integration
Easy Guide to WordPress Theme IntegrationEasy Guide to WordPress Theme Integration
Easy Guide to WordPress Theme Integration
Sankhala Info Solutions
 
Plugin Options
Plugin OptionsPlugin Options
Plugin Options
WordCamp New Zealand
 
Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...
Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...
Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...
cehwitham
 
WordPress theme development from scratch : ICT MeetUp 2013 Nepal
WordPress theme development from scratch : ICT MeetUp 2013 NepalWordPress theme development from scratch : ICT MeetUp 2013 Nepal
WordPress theme development from scratch : ICT MeetUp 2013 Nepal
Chandra Prakash Thapa
 
Custom WordPress theme development
Custom WordPress theme developmentCustom WordPress theme development
Custom WordPress theme development
Tammy Hart
 
WordPress HTML, CSS & Child Themes
WordPress HTML, CSS & Child ThemesWordPress HTML, CSS & Child Themes
WordPress HTML, CSS & Child Themes
Michelle Ames
 
Dreamweaver cs6 step by step
Dreamweaver cs6 step by stepDreamweaver cs6 step by step
Dreamweaver cs6 step by step
zoran Jelinek
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
markparolisi
 
How to Prepare a WordPress Theme for Public Release
How to Prepare a WordPress Theme for Public ReleaseHow to Prepare a WordPress Theme for Public Release
How to Prepare a WordPress Theme for Public Release
David Yeiser
 
Intro to WordPress Child Themes (NERDS Sept 2014)
Intro to WordPress Child Themes (NERDS Sept 2014)Intro to WordPress Child Themes (NERDS Sept 2014)
Intro to WordPress Child Themes (NERDS Sept 2014)
Kelly Dwan
 
WordPress Theme Development: Part 2
WordPress Theme Development: Part 2WordPress Theme Development: Part 2
WordPress Theme Development: Part 2
Josh Lee
 
Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011
Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011
Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011
Georgiana Laudi
 
Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014
Stephanie Eckles
 
WordPress Theme Structure
WordPress Theme StructureWordPress Theme Structure
WordPress Theme Structure
keithdevon
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
Paul Bearne
 
Day of code
Day of codeDay of code
Day of code
Evan Farr
 
Theme Wrangling 101
Theme Wrangling 101Theme Wrangling 101
Theme Wrangling 101
mikeyarce
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
Chandra Prakash Thapa
 
WordPress Theme Development for Designers
WordPress Theme Development for DesignersWordPress Theme Development for Designers
WordPress Theme Development for Designers
elliotjaystocks
 

Tendances (20)

Webiny CMS Starter Guide
Webiny CMS Starter GuideWebiny CMS Starter Guide
Webiny CMS Starter Guide
 
Easy Guide to WordPress Theme Integration
Easy Guide to WordPress Theme IntegrationEasy Guide to WordPress Theme Integration
Easy Guide to WordPress Theme Integration
 
Plugin Options
Plugin OptionsPlugin Options
Plugin Options
 
Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...
Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...
Making WordPress Your CMS and Automatically Updating a Self Hosted WordPress ...
 
WordPress theme development from scratch : ICT MeetUp 2013 Nepal
WordPress theme development from scratch : ICT MeetUp 2013 NepalWordPress theme development from scratch : ICT MeetUp 2013 Nepal
WordPress theme development from scratch : ICT MeetUp 2013 Nepal
 
Custom WordPress theme development
Custom WordPress theme developmentCustom WordPress theme development
Custom WordPress theme development
 
WordPress HTML, CSS & Child Themes
WordPress HTML, CSS & Child ThemesWordPress HTML, CSS & Child Themes
WordPress HTML, CSS & Child Themes
 
Dreamweaver cs6 step by step
Dreamweaver cs6 step by stepDreamweaver cs6 step by step
Dreamweaver cs6 step by step
 
WordPress Structure and Best Practices
WordPress Structure and Best PracticesWordPress Structure and Best Practices
WordPress Structure and Best Practices
 
How to Prepare a WordPress Theme for Public Release
How to Prepare a WordPress Theme for Public ReleaseHow to Prepare a WordPress Theme for Public Release
How to Prepare a WordPress Theme for Public Release
 
Intro to WordPress Child Themes (NERDS Sept 2014)
Intro to WordPress Child Themes (NERDS Sept 2014)Intro to WordPress Child Themes (NERDS Sept 2014)
Intro to WordPress Child Themes (NERDS Sept 2014)
 
WordPress Theme Development: Part 2
WordPress Theme Development: Part 2WordPress Theme Development: Part 2
WordPress Theme Development: Part 2
 
Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011
Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011
Moving from Wordpress.com to Wordpress.org - Wordcamp Toronto 2011
 
Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014Content-Driven WordPress Development - WordCamp Omaha 2014
Content-Driven WordPress Development - WordCamp Omaha 2014
 
WordPress Theme Structure
WordPress Theme StructureWordPress Theme Structure
WordPress Theme Structure
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
 
Day of code
Day of codeDay of code
Day of code
 
Theme Wrangling 101
Theme Wrangling 101Theme Wrangling 101
Theme Wrangling 101
 
Rebrand WordPress Admin
Rebrand WordPress AdminRebrand WordPress Admin
Rebrand WordPress Admin
 
WordPress Theme Development for Designers
WordPress Theme Development for DesignersWordPress Theme Development for Designers
WordPress Theme Development for Designers
 

Similaire à WordPress theme setting page

Your first word press site
Your first word press siteYour first word press site
Your first word press site
Marc Gratch
 
Customizing WordPress Themes
Customizing WordPress ThemesCustomizing WordPress Themes
Customizing WordPress Themes
Domestic Equity Studio
 
Wordpress theme development
Wordpress theme developmentWordpress theme development
Wordpress theme development
Naeem Junejo
 
Most widely used WordPress tips and tricks of 2016
Most widely used WordPress tips and tricks of 2016Most widely used WordPress tips and tricks of 2016
Most widely used WordPress tips and tricks of 2016
Reegan
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
Amanda Giles
 
9 Essential Wordpress Plugins for a Professional Wordpress Blog
9 Essential Wordpress Plugins for a Professional Wordpress Blog9 Essential Wordpress Plugins for a Professional Wordpress Blog
9 Essential Wordpress Plugins for a Professional Wordpress Blog
Ivan Bayross
 
Tablepress - WordPress plugin on inserting Tables
Tablepress - WordPress plugin on inserting TablesTablepress - WordPress plugin on inserting Tables
Tablepress - WordPress plugin on inserting Tables
Thomas Carney
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
Amanda Giles
 
WordPress Webinar Training Presentation
WordPress Webinar Training PresentationWordPress Webinar Training Presentation
WordPress Webinar Training Presentation
MayeCreate Design
 
Word press interview question and answer tops technologies
Word press interview question and answer   tops technologiesWord press interview question and answer   tops technologies
Word press interview question and answer tops technologies
TOPS Technologies
 
Creating a Website with WordPress.org
Creating a Website with WordPress.orgCreating a Website with WordPress.org
Creating a Website with WordPress.org
Eileen Lonergan
 
Chapter 6 the django admin site
Chapter 6  the django admin siteChapter 6  the django admin site
Chapter 6 the django admin site
家璘 卓
 
Wordpress website development
Wordpress website developmentWordpress website development
Wordpress website development
John Faust
 
How to make a website part2
How to make a website part2 How to make a website part2
How to make a website part2
Online Business Owners
 
WordPress Menus - Melbourne User Meetup
WordPress Menus - Melbourne User MeetupWordPress Menus - Melbourne User Meetup
WordPress Menus - Melbourne User Meetup
Chris Burgess
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012
Stephanie Leary
 
Chapter 5
Chapter 5Chapter 5
uCoz: website creation. Chapter 5.
uCoz: website creation. Chapter 5.uCoz: website creation. Chapter 5.
uCoz: website creation. Chapter 5.
Irina Cherepanova
 
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon DublinCreating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Suzanne Dergacheva
 
Introduction to Django CMS
Introduction to Django CMS Introduction to Django CMS
Introduction to Django CMS
Pim Van Heuven
 

Similaire à WordPress theme setting page (20)

Your first word press site
Your first word press siteYour first word press site
Your first word press site
 
Customizing WordPress Themes
Customizing WordPress ThemesCustomizing WordPress Themes
Customizing WordPress Themes
 
Wordpress theme development
Wordpress theme developmentWordpress theme development
Wordpress theme development
 
Most widely used WordPress tips and tricks of 2016
Most widely used WordPress tips and tricks of 2016Most widely used WordPress tips and tricks of 2016
Most widely used WordPress tips and tricks of 2016
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
9 Essential Wordpress Plugins for a Professional Wordpress Blog
9 Essential Wordpress Plugins for a Professional Wordpress Blog9 Essential Wordpress Plugins for a Professional Wordpress Blog
9 Essential Wordpress Plugins for a Professional Wordpress Blog
 
Tablepress - WordPress plugin on inserting Tables
Tablepress - WordPress plugin on inserting TablesTablepress - WordPress plugin on inserting Tables
Tablepress - WordPress plugin on inserting Tables
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
WordPress Webinar Training Presentation
WordPress Webinar Training PresentationWordPress Webinar Training Presentation
WordPress Webinar Training Presentation
 
Word press interview question and answer tops technologies
Word press interview question and answer   tops technologiesWord press interview question and answer   tops technologies
Word press interview question and answer tops technologies
 
Creating a Website with WordPress.org
Creating a Website with WordPress.orgCreating a Website with WordPress.org
Creating a Website with WordPress.org
 
Chapter 6 the django admin site
Chapter 6  the django admin siteChapter 6  the django admin site
Chapter 6 the django admin site
 
Wordpress website development
Wordpress website developmentWordpress website development
Wordpress website development
 
How to make a website part2
How to make a website part2 How to make a website part2
How to make a website part2
 
WordPress Menus - Melbourne User Meetup
WordPress Menus - Melbourne User MeetupWordPress Menus - Melbourne User Meetup
WordPress Menus - Melbourne User Meetup
 
The WordPress University 2012
The WordPress University 2012The WordPress University 2012
The WordPress University 2012
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
uCoz: website creation. Chapter 5.
uCoz: website creation. Chapter 5.uCoz: website creation. Chapter 5.
uCoz: website creation. Chapter 5.
 
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon DublinCreating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
Creating Layouts and Landing Pages for Drupal 8 - DrupalCon Dublin
 
Introduction to Django CMS
Introduction to Django CMS Introduction to Django CMS
Introduction to Django CMS
 

Dernier

Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Diana Rendina
 

Dernier (20)

Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
Reimagining Your Library Space: How to Increase the Vibes in Your Library No ...
 

WordPress theme setting page

  • 1. Create a Settings Page For Your WordPress Theme Junejo Naeem Ahmed WORDPRESS
  • 2. WORDPRESS THEME • Creating your own theme for WordPress is a great way to give your blog or other WordPress powered web site an original touch. • But even the nicest looking theme is not that nice if you have to get under the hood and edit the theme's HTML or PHP code whenever it's time change some aspects of it. • Especially, when it's not you but a paying customer using your theme. • Luckily, creating a settings page for your theme in WordPress is not very hard!
  • 3. DECIDING WHAT SETTINGS ARE NEEDED • It all starts from the need: to create a clear and useful settings page, you have to figure out the things that will need to be changed and leave out everything else. • Every new setting you add to the admin menus adds complexity to the user interface and risks making the theme harder to use. • That's why it's better to be careful and handpick the options that are going to be changed often and leave out one time customizations that can easily be done changing one file inside the theme. • Another question to keep in mind is "Who is going to be changing these settings?“ • If the user is familiar with PHP and WordPress, it might be reasonable to expect that he is OK with embedding his Google Analytics code in the code herself. • but you shouldn't require that from a graphic designer, not to mention a writer who doesn't even need to know anything about HTML and CSS.
  • 4. DECIDING WHAT SETTINGS ARE NEEDED • Common ideas for things to define in theme settings include: • The site's Google Analytics tracking code • The number of sidebars and their positioning (left, right, maybe even up and down) • Page width • The contents of your footer • Options for features that are specific to the theme, such as custom teaser formats.
  • 5. DECIDING WHAT SETTINGS ARE NEEDED • Once you have collected the list of theme features that you'd like to control through a settings page, you are almost ready to start the implementation. • Before you move on and create your settings page, you can save time by making sure that there isn't a WordPress feature already available for the customization you have in mind. • Widgets, custom menus, custom backgrounds and header images are all useful tools for making your theme customizable with a lot less work than required for creating your own settings.
  • 6. HOOKING THE SETTINGS PAGE TO WORDPRESS • Creating a settings page starts by creating a function that sets up the menu and hooking it to the WordPress action admin_menu. • This tells WordPress to call your function when its time to create the menus so that everything is done at its proper time. • Add this code to your theme's functions.php file: • function setup_theme_admin_menus() { • // We will write the function contents very soon. • } • // This tells WordPress to call the function named "setup_theme_admin_menus" • // when it's time to create the menu pages. • add_action("admin_menu", "setup_theme_admin_menus");
  • 7. HOOKING THE SETTINGS PAGE TO WORDPRESS • We'll now put the code for creating the settings pages inside the function we just created. • When creating your settings page, you have the choice of either adding the page as a submenu to one of the existing settings groups or of creating your own top level menu. • Adding a submenu is done with the function add_submenu_page: • <?php add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function) ?>
  • 8. HOOKING THE SETTINGS PAGE TO WORDPRESS • $parent_slug is a unique identifier for the top menu page to which this submenu is added as a child. • $page_title is the title of the page to be added • $menu_title is the title shown in the menu (often a shorter version of $page_title • $capability is the minimum capability required from a user in order to have access to this menu. • $menu_slug is a unique identifier for the menu being created • $function is the name of a function that is called to handle (and render) this menu page.
  • 9. HOOKING THE SETTINGS PAGE TO WORDPRESS • If you choose to add the menu page as a submenu to one of the WordPress groups, you can use the following values as the $parent_slug parameter: • Dashboard: index.php • Posts: edit.php • Media: upload.php • Links: link-manager.php • Pages: edit.php?post_type=page • Comments: edit-comments.php • Appearance: themes.php • Plugins: plugins.php • Users: users.php • Tools: tools.php • Settings: options-general.php
  • 10. HOOKING THE SETTINGS PAGE TO WORDPRESS • The Appearance group looks like a good candidate for placing our settings page. • Let's try that, and create our first settings page. • Here's an updated version of our menu setup function: • function setup_theme_admin_menus() { • add_submenu_page('themes.php', • 'Front Page Elements', 'Front Page', 'manage_options', • 'front-page-elements', 'theme_front_page_settings'); • } • We still need to create the function theme_front_page_settings for this to work. Here it is in its simplest form: • function theme_front_page_settings() { • echo "Hello, world!"; • }
  • 11. HOOKING THE SETTINGS PAGE TO WORDPRESS
  • 12. HOOKING THE SETTINGS PAGE TO WORDPRESS • We also need to check that the user has the rights required for editing the settings page. • To do that, add the following code at the beginning of the settings page function: • // Check that the user is allowed to update options • if (!current_user_can('manage_options')) { • wp_die('You do not have sufficient permissions to access this page.'); • } • Now, if a user who isn't allowed to manage options comes to the settings page, he will see nothing but the message, "You do not have sufficient permissions to access this page."
  • 13. HOOKING THE SETTINGS PAGE TO WORDPRESS • If your theme needs multiple settings pages, it can be confusing for the user to look for them scattered all around the menu structure. • In that case, creating your own settings group makes it easier for the theme user to find all the menu pages for the theme. • To add your own settings group, you need to create a top level menu page and link the submenu pages to it. • Here is a new version of our menu setup function. • The add_menu_page function used to create the top level menu is similar to add_submenu_page except that it doesn't take the $parent_slug parameter.
  • 14. HOOKING THE SETTINGS PAGE TO WORDPRESS • function setup_theme_admin_menus() { • add_menu_page('Theme settings', 'Example theme', 'manage_options', • ‘my_theme_settings', 'theme_settings_page'); • • add_submenu_page(‘my_theme_settings', • 'Front Page Elements', 'Front Page', 'manage_options', • 'front-page-elements', 'theme_front_page_settings'); • } • • // We also need to add the handler function for the top level menu • function theme_settings_page() { • echo "Settings page"; • }
  • 15. HOOKING THE SETTINGS PAGE TO WORDPRESS • If you test the code and refresh the WordPress admin, you'll see your new menu group appear at the bottom of the menu list:
  • 16. HOOKING THE SETTINGS PAGE TO WORDPRESS • But something doesn't look quite right yet. • Clicking the top menu element doesn't lead you to the "Front Page" menu but a menu page called "Example theme.“ • This is not consistent with how the other WordPress menus function, so let's do one more thing: by changing the $menu_slug attribute in the add_submenu_page call to the same value as in the top level menu, we can link the two menus so that selecting the top menu selects the front page menu: function setup_theme_admin_menus() { add_menu_page('Theme settings', 'Example theme', 'manage_options', ‘my_theme_settings', 'theme_settings_page'); add_submenu_page(‘my_theme_settings', 'Front Page Elements', 'Front Page', 'manage_options', ‘my_theme_settings', 'theme_front_page_settings'); } function theme_settings_page() { }
  • 17. HOOKING THE SETTINGS PAGE TO WORDPRESS • Looks better. If you want to still improve the looks of your menu group, there are two optional fields in the add_menu_page function that you will find useful. • Just add the values after the function name in the method call: • $icon_url specifies the URL of an icon for the top level menu. • $position specifies the position of your menu group in the menu list. • The higher the value, the lower the position in the menu.
  • 18. CREATING THE HTML FORM FOR THE SETTINGS PAGES • Now that we have created the settings page, and it shows up nicely in the side menu, it's time to start adding some content. • So, let's go back to the list of settings we had in mind, and draft a page for editing them. • we need a field for defining how many elements should be listed on one row, and a list for defining the actual elements. • To start from the easier, let's create a text field for the number of elements on one row.
  • 19. CREATING THE HTML FORM FOR THE SETTINGS PAGES • Edit your settings page function: • function theme_front_page_settings() { • ?> • <label for="num_elements"> • Number of elements on a row: • </label> • • <input type="text" name="num_elements" /> • <?php • }
  • 20. CREATING THE HTML FORM FOR THE SETTINGS PAGES • When you reload your settings page, you'll see the first settings field appear:
  • 21. CREATING THE HTML FORM FOR THE SETTINGS PAGES • To make the settings page fit seamlessly in the WordPress experience and to give your theme a professional touch, it's a best practice to use the CSS classes and styles that WordPress uses in its own settings pages. • A good way to learn the tricks is to just go ahead and analyze the WordPress source code. • The most important thing is to wrap your setting page with a div with the class "wrap". • Within that div element, you can use many predefined styles such as headings, buttons, and form fields. • Let's start by styling the title of our settings page: • We will create a h2 heading for the page (You can use the heading tags from h2 to h6 to create headings with different sizes.) • We will show the theme settings page icon before the heading. (You can use the predefined WordPress icons with the screen_icon function. • The function can take one of the following parameters: index, edit, upload, link- manager, pages, comments, themes, plugins, users, tools or options-general.) • We will put the input element inside a form and a table with the class form-table.
  • 22. CREATING THE HTML FORM FOR THE SETTINGS PAGES • function theme_front_page_settings() { • ?> • <div class="wrap"> • <?php screen_icon('themes'); ?> <h2>Front page elements</h2> • • <form method="POST" action=""> • <table class="form-table"> • <tr valign="top"> • <th scope="row"> • <label for="num_elements"> • Number of elements on a row: • </label> • </th> • <td> • <input type="text" name="num_elements" size="25" /> • </td> • </tr> • </table> • </form> • </div> • <?php • }
  • 23. CREATING THE HTML FORM FOR THE SETTINGS PAGES
  • 24. SAVING THE FORM • The settings page looks good, but there is something missing: it doesn't do anything yet. • It's time to save some data. • WordPress provides an easy system for saving theme and plugin settings as key value pairs to the database using two functions: • get_option and update_option. • The data stored using the functions can be as simple as a number value or as complex as an array nested multiple times. • The handling of the form is done in the same function that renders the form. • To know whether a form was submitted or not, we add a hidden field, update_settings to the form and then check whether that field was sent or not in the handling function.
  • 25. SAVING THE FORM • if (isset($_POST["update_settings"])) { • // Do the saving • } • The hidden field that goes inside the form looks like this: • <input type="hidden" name="update_settings" value="Y" />
  • 26. SAVING THE FORM • Let's start by saving the easier setting, num_elements. • We'll escape the attribute to make sure the user isn't sending malicious content in the from of HTML tags and then save it to the WordPress settings storage. • When using update_option, we don't need to worry about whether the setting has already been saved or not. • $num_elements = esc_attr($_POST["num_elements"]); • update_option("theme_name_num_elements", $num_elements);
  • 27. SAVING THE FORM • Before we move to saving the list, let's add the current value of num_elements to the settings form so that the user always sees what value he has entered in before deciding the next value. • This will also help us test that the value was actually saved. • <input type="text" name="num_elements" value="<?php echo $num_elements;?>" size="25" /> • And for cases where we haven't saved anything yet, we'll need to load the current value from options, so let's add this piece of code to be executed when there is no form submitted. • $num_elements = get_option("theme_name_num_elements");
  • 28. SAVING THE FORM • When a form is saved, it's important to notify the user so that he isn't left wondering whether something happened or not. • So, let's render a simple notice saying "Settings saved." right after the update_option: • <div id="message" class="updated">Settings saved</div>
  • 29. USING THE SETTINGS INSIDE THE THEME • Saving and showing settings values within the admin area is great, but what really counts is how you use them to customize your theme, so now, we have come to the point where it's time to take our settings and do something cool with them. • From here on, changes go to index.php instead of functions.php. First, we'll read the options to variables: • <?php • $num_elements = get_option("theme_name_num_elements"); • ?>
  • 30. CONCLUSION • Now, we have created a settings page for a custom theme. • The theme is far from complete, but I hope this introduction got you started with adding settings and customizable elements to your next WordPress theme.