Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Wordpress Plugins best practices

Wordpress Plugins best practices

romulodl

July 09, 2012
Tweet

Other Decks in Programming

Transcript

  1. Why so many people use WP? - Easy to install;

    - Easy to create/manage content; Wednesday, 4 July 12
  2. Why so many people use WP? - Easy to install;

    - EASY TO DEVELOP PLUGINS!!!1 - Easy to create/manage content; Wednesday, 4 July 12
  3. BLOGGERS => \o/ Why so many people use WP? -

    Easy to install; - EASY TO DEVELOP PLUGINS!!!1 - Easy to create/manage content; Wednesday, 4 July 12
  4. Why so many people use WP? BLOGGERS => \o/ -

    Easy to install; - EASY TO DEVELOP PLUGINS!!!1 DEVELOPERS => \o/ - Easy to create/manage content; Wednesday, 4 July 12
  5. WP Installation . |- wp-admin/ |- wp-app.php |- wp-atom.php |-

    wp-blog-header.php |- wp-config.php |- wp-content/ |- wp-feed.php |- wp-includes/ |- wp-login.php Wednesday, 4 July 12
  6. WP Installation . |- wp-blah/ |- blah-blah.php |- blah-blah.php |-

    blah-blah.php |- wp-config.php |- wp-content/ |- blah-blah.php |- blah-blah/ |- blah-blah.php Wednesday, 4 July 12
  7. WP Installation . |- wp-blah/ |- blah-blah.php |- blah-blah.php |-

    blah-blah.php |- wp-config.php |- wp-content/plugins/ |- blah-blah.php |- blah-blah/ |- blah-blah.php Wednesday, 4 July 12
  8. - Well documented; - Basics hooks to make your life

    easier; - Organised and Easy to follow; Wednesday, 4 July 12
  9. - Well documented; - Basics hooks to make your life

    easier; - Activation; - Organised and Easy to follow; Wednesday, 4 July 12
  10. - Well documented; - Basics hooks to make your life

    easier; - Activation; - Deactivation; - Organised and Easy to follow; Wednesday, 4 July 12
  11. - Well documented; - Basics hooks to make your life

    easier; - Activation; - Deactivation; - Css and Js embed; - Organised and Easy to follow; Wednesday, 4 July 12
  12. - Well documented; - Basics hooks to make your life

    easier; - Activation; - Deactivation; - Css and Js embed; YOU DON’T NEED TO GOOGLE NOR OPEN THAT OLD PLUGIN TO REMEMBER HOW TO DO IT!!! - Organised and Easy to follow; Wednesday, 4 July 12
  13. Creating your Plugin <?php /* Plugin Name: My Plugin Plugin

    URI: http://trololololololololololo.com/ Description: Example Plugin! Version: 1.0 Author: Romulo De Lazzari Author URI: http://pudim.com.br/ Author Email: [email protected] Edit the plugin’s name Wednesday, 4 July 12
  14. Creating your Plugin - TA-DAH! It’s Running!!! - ...but this

    plugin doesn’t do anything! Wednesday, 4 July 12
  15. Creating your Plugin - TA-DAH! It’s Running!!! - ...but this

    plugin doesn’t do anything! YET! Wednesday, 4 July 12
  16. Unit Tests Unit Tests |- wp-content/plugins/my_plugin/phpunit.xml <?xml version="1.0" encoding="UTF-8"?> <phpunit

    backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false" bootstrap="tests/bootstrap.php" > <testsuites> <testsuite name="MyPlugin Test Suite"> <directory>./tests/</directory> </testsuite> </testsuites> </phpunit> Wednesday, 4 July 12
  17. Unit Tests Unit Tests |- wp-content/plugins/my_plugin/tests/bootstrap.php <?php // Load WordPress

    test environment // https://github.com/nb/wordpress-tests // // The path to wordpress-tests $path = '/path/to/wordpress-tests/init.php'; if( file_exists( $path ) ) { require_once $path; } else { exit( "Couldn't find path to wordpress-tests/init.php\n" ); } Wednesday, 4 July 12
  18. Unit Tests Unit Tests |- /path/to/wordpress-tests/unittests-config.php <?php /* Path to

    the WordPress codebase you'd like to test. Add a backslash in the end. */ define( 'ABSPATH', '/path/to/your/site' ); define( 'DB_NAME', 'wordpress_tests' ); define( 'DB_USER', 'your_username' ); define( 'DB_PASSWORD', 'your_password' ); define( 'DB_HOST', 'localhost' ); define( 'DB_CHARSET', 'utf8' ); define( 'DB_COLLATE', '' ); define( 'WPLANG', '' ); define( 'WP_DEBUG', true ); define( 'WP_DEBUG_DISPLAY', true ); /* Cron tries to make an HTTP request to the blog, which always fails, because tests are run in CLI mode only */ define( 'DISABLE_WP_CRON', true ); $table_prefix = 'wp_'; Wednesday, 4 July 12
  19. Unit Tests Unit Tests |- /wp-content/plugins/my_plugin/tests/my_plugin/ my_plugin_test.php <?php /** *

    Tests */ class MyPluginTest extends WP_UnitTestCase { public $plugin_slug = 'my_plugin'; public function setUp() { parent::setUp(); $this->my_plugin = $GLOBALS['my_plugin']; } public function testPostTitle() { $this->go_to('http://mysite.com/?p=1'); global $wp_query; $post = $wp_query->get_queried_object(); $this->assertEquals('Hello world!', $post->post_title ); } } Wednesday, 4 July 12
  20. Actions and Filters - In your constructor, you have to

    define when your code should act; Wednesday, 4 July 12
  21. Actions and Filters - Actions are points in the execution

    of a page or process lifecycle that WordPress fires. - In your constructor, you have to define when your code should act; Wednesday, 4 July 12
  22. Actions and Filters - Actions are points in the execution

    of a page or process lifecycle that WordPress fires. - In your constructor, you have to define when your code should act; - add_action( 'publish_post', array( $this, 'send_email' ) ); Wednesday, 4 July 12
  23. Actions and Filters - Filters are points of execution in

    which WordPress modifies data before saving it or sending it to the browser. Wednesday, 4 July 12
  24. Actions and Filters - Filters are points of execution in

    which WordPress modifies data before saving it or sending it to the browser. - add_filter( 'the_title', array( $this, 'add_italic' ) ); Wednesday, 4 July 12
  25. More info - https://github.com/tommcfarlin/WordPress-Widget-Boilerplate (all WP hooks list) http://adambrown.info/p/wp_hooks (widget

    template) https://github.com/Automattic/developer/ (developer’s WP plugin) Wednesday, 4 July 12