SlideShare ist ein Scribd-Unternehmen logo
1 von 19
WordPress Structureand Best Practices Mark Parolisi 04-05-2010
Directory Structure Application Directory core files (wp-settings.php, wp-config.php, etc) /wp-admin Operates as a micro-site to control the app with its own css, js, includes directories /wp-includes classes, libraries, scripts for entire app,  JS libraries,  images /wp-content /plugins Can either be directories for large plugins, or just single files /themes Directories of themes /uploads Typically organized by year/month of upload /upgrade
Core The only files that need to be edited are: wp-config.php database connection define constants to override DB values wp-settings.php memory limit (32M default) debug mode DO NOT EDIT OTHER FILES! When we do core updates, these files may be overwritten and your changes would be lost. I have yet to find a ‘core-hack’ that I cannot reproduce through acceptable WordPressplugin conventions.
Database Structure wp_comments wp_commentmeta wp_links Not what you think. It’s just a place for WP to store the links defined by the user in the admin panel wp_options Kitchen sink table that holds everything from site-url to date/time formats, to timestamps for app core/plugin updates Gets very abused bloated with plugin settings due to the ease of CRUD operatios on this table wp_postmeta Holds all extra (custom) data about a post/page.  wp_posts Holds all necessary* data about a post/page wp_terms Defines categories, tags, link categories and custom taxonomies.  Depends on terms_relationships and term_taxonomy. wp_term_relationships wp_term_taxonomy Defines type of taxonomy and contains data about the term(post count, description, etc) wp_usermeta wp_users
Credit to @xentek
Anatomy of a Plugin Files in the /wp-content/plugins directory are scanned for this comment at the start of the document: /* Plugin Name: GMaps Widget Plugin URI: http://wordpress.org/extend/plugins/ Description:  Adds a static Google map with text and a link to full Google Map. Version:  1.0 */ Plugins work by creating our custom functions to do the work, then calling them to fire through native WP functions. We can make a new DB table when the plugin is activated in the admin menu register_activation_hook(__FILE__, ‘myplugin_activation'); function myplugin_activation() { global $wpdb;     $table_name = 'wp_myplguin_table';     if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {        $sql = "CREATE TABLE " . $table_name . " ( alt_title VARCHAR(255) NULL, alt_text TEXT NULL 		);";         require(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql);     } }
Actionsadd_action(‘wp_footer’, ‘our_custom_function’) When WP loads pages (client or admin), it sets action hooks which we can use to load our scripts. Currently 30 for front-end, 11 for admin. set_current_user template_redirect wp_head loop_start Use these hooks to prevent conflicts and set dependencies. By calling the wp_enqueue_script method (for JS) at the wp_enqueue_scripts action, we make sure that we aren’t loading scripts twice and the script is loaded in the right order (very important for JS)  WordPress also allows us to create our own actions hooks.  http://codex.wordpress.org/Plugin_API/Action_Reference
Filtersadd_filter(‘the_content’,‘our_function_name’) Filters are very similar to actions in that WP sets them for various elements in the application. But rather than just setting hooks for our functions, they allow us to alter (filter) the original data. the_content wp_list_pages Example: Adding to original post content 	functionaddContent($content = ''){	$content .= "<p>My plugin text</p>";    return $content;} add_filter(‘the_content’, ‘addContent’); Filters can be used not only to append content but also remove, organize and modify it (parental-filters, custom sort for navigation menus, etc) http://codex.wordpress.org/Plugin_API/Filter_Reference
HackingModding other Plugins Open Source plugins are free to use and modify in your own environment. But do it with care. Sometimes you can actually create a new plugin that alters another plugin (very case-by-case) Decide whether your mod is worth losing support from the native plugin. Change the plugin name or version number to prevent accidentally update and overwriting of your changes. Document/Comment all of your changes. If the mod could be used by others, try to contact the original author and share your patch.
Widgets Widgets are small bits of functionality that run in special areas of a template called ‘widgetized areas’ (formerly ‘sidebars’) Widgets can be created in plugin files or the theme’s functions.php file. Widget structure is pretty basic Class SampleWidget extends WP_Widget{     function SampleWidget(){         parent::WP_Widget(false, $name = ‘SampleWidget');	     } function widget($args, $instance){		         //what the widget will output } function update($new_instance, $old_instance){ 	//updating the values of the widget from the form function } function form($instance){	 	//make the form that appears in the /wp-admin widgets section } } //end class add_action('widgets_init', create_function('', 'return register_widget(" SampleWidget");')); http://codex.wordpress.org/Widgets_API
Theme Templates A standard WP theme contains the following views Header.php Index.php Sidebar.php Archive.php Single.php Page.php Search.php Footer.php Comments.php Functions.php Some of these files are optional -- e.g. If you omit single.php the index.php will render the content.
Functions.php This is the first file loaded and acts just like a plugin file. Anything you can do with a plugin, you can localize to a theme with functions.php This file typically defines widget areas, loading of custom JS and CSS, and the creation of custom admin menus and logic for the theme. If you find yourself writing functions into a template view, STOP! Write the logic in the functions.php file and just call it in the template. If your functions.php file becomes unmanageably large, don’t be afraid to break apart the logic with includes.
Content Templates Index.php The initial ‘home page’ content. Default loads the most recent posts. Page.php Loads content classified as ‘pages’ ?page_id=2 Archive.php Loads posts from a specific group/taxonomy.  Categories Tags Authors Dates Single.php Loads content from a single ‘post’
The Loop When a content page loads a query based on what kind of template it is (archive, single, page) run. This primary query is accessed by “The Loop” if ( have_posts() ) : while ( have_posts() ) : the_post(); //call our different template tags to retrieve data (title, date, post_content) endwhile; else: //default if we have no posts in the loop  endif;  You can alter this main query by pre-pending the loop with the query_posts() function. query_posts(‘orderby=title&order=ASC'); query_posts() accepts many parameters. http://codex.wordpress.org/Function_Reference/query_posts
Retrieving Data about our Post(s) Template tags are functions that run within ‘The Loop’ and echo back data. the_title() the_content() the_permalink()  There are also value-returning equivalents to most of these functions. get_title() get_permalink() The $wp_query object (outside the loop or extra data) All of the data about the loop is stored in this object Within this object there are many arrays within this object query_vars, request, comments, post, etc The $post object All of the data about the post is stored in this object $post->comment_count  , $post->post_modified, etc http://codex.wordpress.org/Function_Reference/WP_Query
Running Multiple Queries Sometime we need to run additional queries on our pages (to get related posts perhaps). <?php $related_posts = new WP_Query(‘cat=3&showposts=5'); while ($related_posts->have_posts()) : $related_posts->the_post(); ?> <div class=“related_post">     <h2><?phpthe_title(); ?></h2> 		<?phpthe_excerpt(); ?>     <a href="<?phpthe_permalink(); ?>”>Learn More</a> </div> <?phpendwhile; wp_reset_query(); ?> WP_Query accepts the same arguments as query_posts() http://codex.wordpress.org/Function_Reference/query_posts Note that you can query posts from post/page name or category name but I generally resist this approach due to the possibility of error. Use wp_reset_query() to return back to the original query
What NOT to do in Templates Get, don’t build URL’s <a href="'.get_bloginfo('url').'/category/'.$ctBX->category_nicename.‘”> get_category_link($id) or get_permalink($id) Not checking if plugin function exists if(function_exists(‘plugin_function’)): plugin_function(); endif; Don’t use URL to find location if(isset($_GET[‘s’]) || $_SERVER[‘REQUEST_URI’] == ‘/index.php’) if(is_search() || is_home()) Calling JS or CSS from template ex. Loading jQuery from the template will NOT ensure that another plugin doesn’t load jQuery too. Sometimes different versions lead to conflicts and errors. Should use the wp_enqueue_script and wp_enqueue_style in functions.php
Things to Remember Having modular plugins that can work with any theme pays off in the long run. Never write a plugin to be site-specific. Build it to be flexible and scalable, or let that feature exist only in the functions.php file of the parent theme. Treat your theme files as views. That’s what they are so don’t make them more than that.  Always add classes and/or id’s to elements to ensure that front-end styling is easy and effective. If you find yourself copying/pasting snippets of code into theme files (like RSS feeds, videos, etc) turn that snippet into a widget and maintain only one piece of code. Keeping core WordPress files and DB schema current is easy when following WP standards. Running the latest versions not only adds new features, but fixes bugs and security holes. Remember that WordPress was not built for developers; it was built for users.  The easy learning curve allows for most people to quickly start adding content to a website, but it is the responsibility of the developer to be the guardian of the output.  Remove all functional-level options from the user and let them access only what they are interested in—the content.  If aplugin requires multiple user-selected conditions to be fulfilled or code to be written into a template for it to even work, then it doesn’t work for WordPress. Ultimately, WordPress is a platform that encourages its developers to innovate by creating an open system than can easily be manipulated, but we can’t do it at the cost of stability, scalability, and usability of our products.

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to WordPress Theme Development
Introduction to WordPress Theme DevelopmentIntroduction to WordPress Theme Development
Introduction to WordPress Theme DevelopmentSitdhibong Laokok
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress developmentSteve Mortiboy
 
Take Command of WordPress With WP-CLI at WordCamp Long Beach
Take Command of WordPress With WP-CLI at WordCamp Long BeachTake Command of WordPress With WP-CLI at WordCamp Long Beach
Take Command of WordPress With WP-CLI at WordCamp Long BeachDiana Thompson
 
CSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsCSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsDougal Campbell
 
Child Themes in WordPress
Child Themes in WordPressChild Themes in WordPress
Child Themes in WordPressJeff Cohan
 
How to Issue and Activate Free SSL using Let's Encrypt
How to Issue and Activate Free SSL using Let's EncryptHow to Issue and Activate Free SSL using Let's Encrypt
How to Issue and Activate Free SSL using Let's EncryptMayeenul Islam
 
Worcamp2012 make a wordpress multisite in 20mins
Worcamp2012 make a wordpress multisite in 20minsWorcamp2012 make a wordpress multisite in 20mins
Worcamp2012 make a wordpress multisite in 20minsChandra Prakash Thapa
 
Chandra Prakash Thapa: Make a WordPress Multisite in 20 mins
Chandra Prakash Thapa: Make a WordPress Multisite in 20 minsChandra Prakash Thapa: Make a WordPress Multisite in 20 mins
Chandra Prakash Thapa: Make a WordPress Multisite in 20 minswpnepal
 
WordPress 2.5 Overview - Rich Media Institute
WordPress 2.5 Overview - Rich Media InstituteWordPress 2.5 Overview - Rich Media Institute
WordPress 2.5 Overview - Rich Media InstituteBrendan Sera-Shriar
 
Take Command of WordPress With WP-CLI
Take Command of WordPress With WP-CLITake Command of WordPress With WP-CLI
Take Command of WordPress With WP-CLIDiana Thompson
 
WordPress for Education PPT
WordPress for Education PPTWordPress for Education PPT
WordPress for Education PPTjekkilekki
 
WordPress Child Themes
WordPress Child ThemesWordPress Child Themes
WordPress Child Themesrfair404
 
5 Things You Shouldn't Do With A WordPress Plugin
5 Things You Shouldn't Do With A WordPress Plugin5 Things You Shouldn't Do With A WordPress Plugin
5 Things You Shouldn't Do With A WordPress PluginKelly Phillips
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1Yoav Farhi
 
Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Joe Querin
 
Advanced WordPress Development Environments
Advanced WordPress Development EnvironmentsAdvanced WordPress Development Environments
Advanced WordPress Development EnvironmentsBeau Lebens
 
Introduction to building joomla! components using FOF
Introduction to building joomla! components using FOFIntroduction to building joomla! components using FOF
Introduction to building joomla! components using FOFTim Plummer
 
Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)
Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)
Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)Andrew Duthie
 

Was ist angesagt? (20)

Extending WordPress
Extending WordPressExtending WordPress
Extending WordPress
 
What is (not) WordPress
What is (not) WordPressWhat is (not) WordPress
What is (not) WordPress
 
Introduction to WordPress Theme Development
Introduction to WordPress Theme DevelopmentIntroduction to WordPress Theme Development
Introduction to WordPress Theme Development
 
Getting started with WordPress development
Getting started with WordPress developmentGetting started with WordPress development
Getting started with WordPress development
 
Take Command of WordPress With WP-CLI at WordCamp Long Beach
Take Command of WordPress With WP-CLI at WordCamp Long BeachTake Command of WordPress With WP-CLI at WordCamp Long Beach
Take Command of WordPress With WP-CLI at WordCamp Long Beach
 
CSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the GutsCSI: WordPress -- Getting Into the Guts
CSI: WordPress -- Getting Into the Guts
 
Child Themes in WordPress
Child Themes in WordPressChild Themes in WordPress
Child Themes in WordPress
 
How to Issue and Activate Free SSL using Let's Encrypt
How to Issue and Activate Free SSL using Let's EncryptHow to Issue and Activate Free SSL using Let's Encrypt
How to Issue and Activate Free SSL using Let's Encrypt
 
Worcamp2012 make a wordpress multisite in 20mins
Worcamp2012 make a wordpress multisite in 20minsWorcamp2012 make a wordpress multisite in 20mins
Worcamp2012 make a wordpress multisite in 20mins
 
Chandra Prakash Thapa: Make a WordPress Multisite in 20 mins
Chandra Prakash Thapa: Make a WordPress Multisite in 20 minsChandra Prakash Thapa: Make a WordPress Multisite in 20 mins
Chandra Prakash Thapa: Make a WordPress Multisite in 20 mins
 
WordPress 2.5 Overview - Rich Media Institute
WordPress 2.5 Overview - Rich Media InstituteWordPress 2.5 Overview - Rich Media Institute
WordPress 2.5 Overview - Rich Media Institute
 
Take Command of WordPress With WP-CLI
Take Command of WordPress With WP-CLITake Command of WordPress With WP-CLI
Take Command of WordPress With WP-CLI
 
WordPress for Education PPT
WordPress for Education PPTWordPress for Education PPT
WordPress for Education PPT
 
WordPress Child Themes
WordPress Child ThemesWordPress Child Themes
WordPress Child Themes
 
5 Things You Shouldn't Do With A WordPress Plugin
5 Things You Shouldn't Do With A WordPress Plugin5 Things You Shouldn't Do With A WordPress Plugin
5 Things You Shouldn't Do With A WordPress Plugin
 
WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1WordPress Developers Israel Meetup #1
WordPress Developers Israel Meetup #1
 
Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015Responsive Theme Workshop - WordCamp Columbus 2015
Responsive Theme Workshop - WordCamp Columbus 2015
 
Advanced WordPress Development Environments
Advanced WordPress Development EnvironmentsAdvanced WordPress Development Environments
Advanced WordPress Development Environments
 
Introduction to building joomla! components using FOF
Introduction to building joomla! components using FOFIntroduction to building joomla! components using FOF
Introduction to building joomla! components using FOF
 
Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)
Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)
Launching a WordPress Site 101 (Cincinnati WordPress, August 2015)
 

Andere mochten auch

εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)
εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)
εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)Theodoros Douvlis
 
Wordpress Workflow
Wordpress Workflow Wordpress Workflow
Wordpress Workflow Filippo Dino
 
Mastering the shortcode api
Mastering the shortcode apiMastering the shortcode api
Mastering the shortcode apiPeter Baylies
 
Consuming & embedding external content in WordPress
Consuming & embedding external content in WordPressConsuming & embedding external content in WordPress
Consuming & embedding external content in WordPressAkshay Raje
 
εισαγωγη στη Joomla 1
εισαγωγη στη Joomla 1εισαγωγη στη Joomla 1
εισαγωγη στη Joomla 1Theodoros Douvlis
 
WordPress best practices by billrice
WordPress best practices by billriceWordPress best practices by billrice
WordPress best practices by billriceRiceDesign
 
Google drive for nonprofits webinar
Google drive for nonprofits webinarGoogle drive for nonprofits webinar
Google drive for nonprofits webinarCraig Grella
 
Social media marketing training blackthorn
Social media marketing training   blackthornSocial media marketing training   blackthorn
Social media marketing training blackthornsauravstudio45
 
Best Friend || Worst Enemy: WordPress Multisite
Best Friend || Worst Enemy: WordPress MultisiteBest Friend || Worst Enemy: WordPress Multisite
Best Friend || Worst Enemy: WordPress MultisiteTaylor McCaslin
 
Osi model
Osi modelOsi model
Osi modelOnline
 
How To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comHow To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comKathy Gill
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011photomatt
 

Andere mochten auch (13)

εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)
εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)
εισαγωγή στα συστήματα διαχείρισης περιεχομένου (Cms)
 
Wordpress Workflow
Wordpress Workflow Wordpress Workflow
Wordpress Workflow
 
Mastering the shortcode api
Mastering the shortcode apiMastering the shortcode api
Mastering the shortcode api
 
Consuming & embedding external content in WordPress
Consuming & embedding external content in WordPressConsuming & embedding external content in WordPress
Consuming & embedding external content in WordPress
 
εισαγωγη στη Joomla 1
εισαγωγη στη Joomla 1εισαγωγη στη Joomla 1
εισαγωγη στη Joomla 1
 
WordPress best practices by billrice
WordPress best practices by billriceWordPress best practices by billrice
WordPress best practices by billrice
 
Google drive for nonprofits webinar
Google drive for nonprofits webinarGoogle drive for nonprofits webinar
Google drive for nonprofits webinar
 
Social media marketing training blackthorn
Social media marketing training   blackthornSocial media marketing training   blackthorn
Social media marketing training blackthorn
 
Best Friend || Worst Enemy: WordPress Multisite
Best Friend || Worst Enemy: WordPress MultisiteBest Friend || Worst Enemy: WordPress Multisite
Best Friend || Worst Enemy: WordPress Multisite
 
Osi model
Osi modelOsi model
Osi model
 
How To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.comHow To Embed SlideShare Shows Into WordPress.com
How To Embed SlideShare Shows Into WordPress.com
 
OSI Model
OSI ModelOSI Model
OSI Model
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
 

Ähnlich wie WordPress Structure Best Practices Guide

Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011David Carr
 
Getting Started With WordPress Development
Getting Started With WordPress DevelopmentGetting Started With WordPress Development
Getting Started With WordPress DevelopmentAndy Brudtkuhl
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Paul Bearne
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme EnlightenmentAmanda Giles
 
Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentTammy Hart
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017Amanda Giles
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsAlessandro Molina
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's CodeWildan Maulana
 
Plugin Development Practices
Plugin Development PracticesPlugin Development Practices
Plugin Development Practicesdanpastori
 
WordPress Theme Workshop: Misc
WordPress Theme Workshop: MiscWordPress Theme Workshop: Misc
WordPress Theme Workshop: MiscDavid Bisset
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentBrad Williams
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin developmentMostafa Soufi
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolGordon Forsythe
 
WordPress Plugin Basics
WordPress Plugin BasicsWordPress Plugin Basics
WordPress Plugin BasicsAmanda Giles
 
Getting to The Loop - London Wordpress Meetup July 28th
Getting to The Loop - London Wordpress Meetup  July 28thGetting to The Loop - London Wordpress Meetup  July 28th
Getting to The Loop - London Wordpress Meetup July 28thChris Adams
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress WebsitesKyle Cearley
 
PSD to WordPress
PSD to WordPressPSD to WordPress
PSD to WordPressNile Flores
 
WordPress as a Content Management System
WordPress as a Content Management SystemWordPress as a Content Management System
WordPress as a Content Management SystemValent Mustamin
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2giwoolee
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkBo-Yi Wu
 

Ähnlich wie WordPress Structure Best Practices Guide (20)

Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011Introduction to Plugin Programming, WordCamp Miami 2011
Introduction to Plugin Programming, WordCamp Miami 2011
 
Getting Started With WordPress Development
Getting Started With WordPress DevelopmentGetting Started With WordPress Development
Getting Started With WordPress Development
 
Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919Childthemes ottawa-word camp-1919
Childthemes ottawa-word camp-1919
 
The Way to Theme Enlightenment
The Way to Theme EnlightenmentThe Way to Theme Enlightenment
The Way to Theme Enlightenment
 
Laying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme developmentLaying the proper foundation for plugin and theme development
Laying the proper foundation for plugin and theme development
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
TurboGears2 Pluggable Applications
TurboGears2 Pluggable ApplicationsTurboGears2 Pluggable Applications
TurboGears2 Pluggable Applications
 
Exploring Symfony's Code
Exploring Symfony's CodeExploring Symfony's Code
Exploring Symfony's Code
 
Plugin Development Practices
Plugin Development PracticesPlugin Development Practices
Plugin Development Practices
 
WordPress Theme Workshop: Misc
WordPress Theme Workshop: MiscWordPress Theme Workshop: Misc
WordPress Theme Workshop: Misc
 
Intro to WordPress Plugin Development
Intro to WordPress Plugin DevelopmentIntro to WordPress Plugin Development
Intro to WordPress Plugin Development
 
WordPress Plugin development
WordPress Plugin developmentWordPress Plugin development
WordPress Plugin development
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
WordPress Plugin Basics
WordPress Plugin BasicsWordPress Plugin Basics
WordPress Plugin Basics
 
Getting to The Loop - London Wordpress Meetup July 28th
Getting to The Loop - London Wordpress Meetup  July 28thGetting to The Loop - London Wordpress Meetup  July 28th
Getting to The Loop - London Wordpress Meetup July 28th
 
Building Potent WordPress Websites
Building Potent WordPress WebsitesBuilding Potent WordPress Websites
Building Potent WordPress Websites
 
PSD to WordPress
PSD to WordPressPSD to WordPress
PSD to WordPress
 
WordPress as a Content Management System
WordPress as a Content Management SystemWordPress as a Content Management System
WordPress as a Content Management System
 
WordPress plugin #2
WordPress plugin #2WordPress plugin #2
WordPress plugin #2
 
CodeIgniter PHP MVC Framework
CodeIgniter PHP MVC FrameworkCodeIgniter PHP MVC Framework
CodeIgniter PHP MVC Framework
 

Kürzlich hochgeladen

Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 

Kürzlich hochgeladen (20)

Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 

WordPress Structure Best Practices Guide

  • 1. WordPress Structureand Best Practices Mark Parolisi 04-05-2010
  • 2. Directory Structure Application Directory core files (wp-settings.php, wp-config.php, etc) /wp-admin Operates as a micro-site to control the app with its own css, js, includes directories /wp-includes classes, libraries, scripts for entire app, JS libraries, images /wp-content /plugins Can either be directories for large plugins, or just single files /themes Directories of themes /uploads Typically organized by year/month of upload /upgrade
  • 3. Core The only files that need to be edited are: wp-config.php database connection define constants to override DB values wp-settings.php memory limit (32M default) debug mode DO NOT EDIT OTHER FILES! When we do core updates, these files may be overwritten and your changes would be lost. I have yet to find a ‘core-hack’ that I cannot reproduce through acceptable WordPressplugin conventions.
  • 4. Database Structure wp_comments wp_commentmeta wp_links Not what you think. It’s just a place for WP to store the links defined by the user in the admin panel wp_options Kitchen sink table that holds everything from site-url to date/time formats, to timestamps for app core/plugin updates Gets very abused bloated with plugin settings due to the ease of CRUD operatios on this table wp_postmeta Holds all extra (custom) data about a post/page. wp_posts Holds all necessary* data about a post/page wp_terms Defines categories, tags, link categories and custom taxonomies. Depends on terms_relationships and term_taxonomy. wp_term_relationships wp_term_taxonomy Defines type of taxonomy and contains data about the term(post count, description, etc) wp_usermeta wp_users
  • 5.
  • 7. Anatomy of a Plugin Files in the /wp-content/plugins directory are scanned for this comment at the start of the document: /* Plugin Name: GMaps Widget Plugin URI: http://wordpress.org/extend/plugins/ Description: Adds a static Google map with text and a link to full Google Map. Version: 1.0 */ Plugins work by creating our custom functions to do the work, then calling them to fire through native WP functions. We can make a new DB table when the plugin is activated in the admin menu register_activation_hook(__FILE__, ‘myplugin_activation'); function myplugin_activation() { global $wpdb; $table_name = 'wp_myplguin_table'; if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { $sql = "CREATE TABLE " . $table_name . " ( alt_title VARCHAR(255) NULL, alt_text TEXT NULL );"; require(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } }
  • 8. Actionsadd_action(‘wp_footer’, ‘our_custom_function’) When WP loads pages (client or admin), it sets action hooks which we can use to load our scripts. Currently 30 for front-end, 11 for admin. set_current_user template_redirect wp_head loop_start Use these hooks to prevent conflicts and set dependencies. By calling the wp_enqueue_script method (for JS) at the wp_enqueue_scripts action, we make sure that we aren’t loading scripts twice and the script is loaded in the right order (very important for JS) WordPress also allows us to create our own actions hooks. http://codex.wordpress.org/Plugin_API/Action_Reference
  • 9. Filtersadd_filter(‘the_content’,‘our_function_name’) Filters are very similar to actions in that WP sets them for various elements in the application. But rather than just setting hooks for our functions, they allow us to alter (filter) the original data. the_content wp_list_pages Example: Adding to original post content functionaddContent($content = ''){ $content .= "<p>My plugin text</p>";    return $content;} add_filter(‘the_content’, ‘addContent’); Filters can be used not only to append content but also remove, organize and modify it (parental-filters, custom sort for navigation menus, etc) http://codex.wordpress.org/Plugin_API/Filter_Reference
  • 10. HackingModding other Plugins Open Source plugins are free to use and modify in your own environment. But do it with care. Sometimes you can actually create a new plugin that alters another plugin (very case-by-case) Decide whether your mod is worth losing support from the native plugin. Change the plugin name or version number to prevent accidentally update and overwriting of your changes. Document/Comment all of your changes. If the mod could be used by others, try to contact the original author and share your patch.
  • 11. Widgets Widgets are small bits of functionality that run in special areas of a template called ‘widgetized areas’ (formerly ‘sidebars’) Widgets can be created in plugin files or the theme’s functions.php file. Widget structure is pretty basic Class SampleWidget extends WP_Widget{ function SampleWidget(){ parent::WP_Widget(false, $name = ‘SampleWidget'); } function widget($args, $instance){ //what the widget will output } function update($new_instance, $old_instance){ //updating the values of the widget from the form function } function form($instance){ //make the form that appears in the /wp-admin widgets section } } //end class add_action('widgets_init', create_function('', 'return register_widget(" SampleWidget");')); http://codex.wordpress.org/Widgets_API
  • 12. Theme Templates A standard WP theme contains the following views Header.php Index.php Sidebar.php Archive.php Single.php Page.php Search.php Footer.php Comments.php Functions.php Some of these files are optional -- e.g. If you omit single.php the index.php will render the content.
  • 13. Functions.php This is the first file loaded and acts just like a plugin file. Anything you can do with a plugin, you can localize to a theme with functions.php This file typically defines widget areas, loading of custom JS and CSS, and the creation of custom admin menus and logic for the theme. If you find yourself writing functions into a template view, STOP! Write the logic in the functions.php file and just call it in the template. If your functions.php file becomes unmanageably large, don’t be afraid to break apart the logic with includes.
  • 14. Content Templates Index.php The initial ‘home page’ content. Default loads the most recent posts. Page.php Loads content classified as ‘pages’ ?page_id=2 Archive.php Loads posts from a specific group/taxonomy. Categories Tags Authors Dates Single.php Loads content from a single ‘post’
  • 15. The Loop When a content page loads a query based on what kind of template it is (archive, single, page) run. This primary query is accessed by “The Loop” if ( have_posts() ) : while ( have_posts() ) : the_post(); //call our different template tags to retrieve data (title, date, post_content) endwhile; else: //default if we have no posts in the loop endif; You can alter this main query by pre-pending the loop with the query_posts() function. query_posts(‘orderby=title&order=ASC'); query_posts() accepts many parameters. http://codex.wordpress.org/Function_Reference/query_posts
  • 16. Retrieving Data about our Post(s) Template tags are functions that run within ‘The Loop’ and echo back data. the_title() the_content() the_permalink() There are also value-returning equivalents to most of these functions. get_title() get_permalink() The $wp_query object (outside the loop or extra data) All of the data about the loop is stored in this object Within this object there are many arrays within this object query_vars, request, comments, post, etc The $post object All of the data about the post is stored in this object $post->comment_count , $post->post_modified, etc http://codex.wordpress.org/Function_Reference/WP_Query
  • 17. Running Multiple Queries Sometime we need to run additional queries on our pages (to get related posts perhaps). <?php $related_posts = new WP_Query(‘cat=3&showposts=5'); while ($related_posts->have_posts()) : $related_posts->the_post(); ?> <div class=“related_post"> <h2><?phpthe_title(); ?></h2> <?phpthe_excerpt(); ?> <a href="<?phpthe_permalink(); ?>”>Learn More</a> </div> <?phpendwhile; wp_reset_query(); ?> WP_Query accepts the same arguments as query_posts() http://codex.wordpress.org/Function_Reference/query_posts Note that you can query posts from post/page name or category name but I generally resist this approach due to the possibility of error. Use wp_reset_query() to return back to the original query
  • 18. What NOT to do in Templates Get, don’t build URL’s <a href="'.get_bloginfo('url').'/category/'.$ctBX->category_nicename.‘”> get_category_link($id) or get_permalink($id) Not checking if plugin function exists if(function_exists(‘plugin_function’)): plugin_function(); endif; Don’t use URL to find location if(isset($_GET[‘s’]) || $_SERVER[‘REQUEST_URI’] == ‘/index.php’) if(is_search() || is_home()) Calling JS or CSS from template ex. Loading jQuery from the template will NOT ensure that another plugin doesn’t load jQuery too. Sometimes different versions lead to conflicts and errors. Should use the wp_enqueue_script and wp_enqueue_style in functions.php
  • 19. Things to Remember Having modular plugins that can work with any theme pays off in the long run. Never write a plugin to be site-specific. Build it to be flexible and scalable, or let that feature exist only in the functions.php file of the parent theme. Treat your theme files as views. That’s what they are so don’t make them more than that. Always add classes and/or id’s to elements to ensure that front-end styling is easy and effective. If you find yourself copying/pasting snippets of code into theme files (like RSS feeds, videos, etc) turn that snippet into a widget and maintain only one piece of code. Keeping core WordPress files and DB schema current is easy when following WP standards. Running the latest versions not only adds new features, but fixes bugs and security holes. Remember that WordPress was not built for developers; it was built for users. The easy learning curve allows for most people to quickly start adding content to a website, but it is the responsibility of the developer to be the guardian of the output. Remove all functional-level options from the user and let them access only what they are interested in—the content. If aplugin requires multiple user-selected conditions to be fulfilled or code to be written into a template for it to even work, then it doesn’t work for WordPress. Ultimately, WordPress is a platform that encourages its developers to innovate by creating an open system than can easily be manipulated, but we can’t do it at the cost of stability, scalability, and usability of our products.