"Mr Branding" is a blog based on RSS for everything related to website branding and website design, it collects its posts from many sites in order to facilitate the updating to the latest technology.
To suggest any source, please contact me: Taha.baba@consultant.com
Saturday, December 31, 2016
Here's What Happened on Instagram in 2016 (infographic)
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Irfan Ahmad via Digital Information World
Periscope Live 360 Video: This Week in Social Media
Welcome to our weekly edition of what’s hot in social media news. To help you stay up to date with social media, here are some of the news items that caught our attention. What’s New This Week Periscope Rolls Out Support for Live 360 Video: Periscope launched its own Live 360 video experience this week. [...]
This post Periscope Live 360 Video: This Week in Social Media first appeared on .
- Your Guide to the Social Media Jungle
by Grace Duffy via
Window Wonderland
by via Awwwards - Sites of the day
Friday, December 30, 2016
14 Best Online Marketplaces for Graphic Design
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Web Desk via Digital Information World
Fajax – Lightweight jQuery Plugin for Ajaxifying Forms
Fajax is a lightweight jQuery plugin for ajaxifying forms with file support. It's wrapper of $.ajax
function.
by via jQuery-Plugins.net RSS Feed
How to Build Raving Fans in Unconventional Ways
Do you want enthusiastic fans? Looking for unique ways to engage your audience? To discover how he grew a large and thriving fan base, I interview Pat Flynn. More About This Show The Social Media Marketing podcast is an on-demand talk radio show from Social Media Examiner. It’s designed to help busy marketers and business owners discover [...]
This post How to Build Raving Fans in Unconventional Ways first appeared on .
- Your Guide to the Social Media Jungle
by Michael Stelzner via
UNIEL ltd.
by via Awwwards - Sites of the day
Thursday, December 29, 2016
LinkedIn: Best Practices for Marketing Professionals (infographic)
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Web Desk via Digital Information World
VisitorParser.js – User Agent & IP Geo Location Parser with Javascript
VisitorParser.js is a lightweight javascript-based to analyze visitors with user-agent & ip geo location parser.
by via jQuery-Plugins.net RSS Feed
Daily
The Daily Project aims to encourage spirited writing and curated exploration through daily writing prompts and our 10-things blog.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Redixel
Redixel was firstly founded in 2013 in Singapore and offers ONLY products at international level, which involved User Experience, Website Creation, Mobile Development, Content Management and Digital Marketing for clients across the world.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Goodricke Tea
Goodricke Limited is synonymous with its gardens that are heralded the world over for more reasons than one. A cup of Goodricke tea is a rare combination of fine taste, unbeatable flavor and generous doses of mystique and history.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
welspunindia
At Welspun, we don’t just weave the best textiles in the world. We weave innovation with people to create stories
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Romain Richard Portfolio
I’m Romain Richard, a graphic and web designer junior, currently working for French Spirit in Shanghai and student in second year at MMI Bordeaux.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Residente
by via Awwwards - Sites of the day
Conversational Interface
by Rob Hope via One Page Love
Creating a Custom WordPress Messaging System, Part 4
In this series, we've taken a look at how we can implement a system that allows us to programmatically define custom messages that display on a given administration page in the WordPress back end.
If you've followed along with the series thus far, then you know:
- We've laid the groundwork for the plugin that's used throughout this series, and even developed it a bit further.
- We've defined and used a custom hook that we can use to render the settings messages.
- We've added support for success, warning, and error messages that can be rendered at the top of a given settings page.
As mentioned in the previous tutorial:
But if you've read any of my previous tutorials, you know that I'm not a fan of having duplicated code. Nor am I fan of having one class do many things. And, unfortunately, that's exactly that we're doing here.
And we're going to address that in this final tutorial. By the end, we'll have a complete refactored solution that uses some intermediate object-oriented principles like inheritance. We'll also have a few methods that we can use programmatically or that can be registered with the WordPress hook system.
Getting Started at the End
At this point you should know exactly what you need in your local development environment. Specifically, you should have the following:
- PHP 5.6.25 and MySQL 5.6.28
- Apache or Nginx
- WordPress 4.6.1
- Your preferred IDE or editor
I also recommend the most recent version of the source code as it will allow you to walk through all of the changes that we're going to make. If you don't have it, that's okay, but I recommend reading back over the previous tutorials before going any further.
In the Previous Tutorial
As you may recall (or have ascertained from the comment above), the previous tutorial left us with a single class that was doing too much work.
One way to know this is that if you were to describe what the class was doing, you wouldn't be able to give a single answer. Instead, you'd have to say that it was responsible for handling success messages, warning messages, error messages, and rendering all of them independently of one another.
And though you might make the case that it was "managing custom messages," you wouldn't necessarily be describing just how verbose the class was. That's what we hope to resolve in this tutorial.
In the Final Tutorial
Specifically, we're going to be looking at doing the following:
- removing the old settings messenger class
- adding a new, more generic settings message class
- adding a settings messenger class with which to communicate
- introducing methods that we can use independent of WordPress
- streamlining how WordPress renders the messages
We have our work cut out for us, so let's go ahead and get started with all of the above.
Refactoring Our Work
When it comes to refactoring our work, it helps to know exactly what it is that we want to do. In our case, we recognize that we have a lot of duplicate code that could be condensed.
Furthermore, we have three different types of messages managed in exactly the same way save for how they are rendered. And in that instance, it's an issue of the HTML class attributes.
Thus, we can generalize that code to focus on a specific type, and we can consolidate a lot of the methods for adding success messages or retrieving error messages by generalizing a method to recognize said type.
Ultimately, we will do that. But first, some housekeeping.
1. Remove the Old Settings Messenger
In the previous tutorials, we've been working with a class called Settings_Messenger
. Up to this point, it has served its purpose, but we're going to be refactoring this class throughout the remainder of this tutorial.
When it comes to this type of refactoring, it's easy to want to simply delete the class and start over. There are times in which this is appropriate, but this is not one of them. Instead, we're going to take that class and refactor what's already there.
All of that to say, don't delete the file and get started with a new one. Instead, track with what we're doing throughout this tutorial.
2. A New Setting Message Class
First, let's introduce a Settings_Message
class. This represents any type of settings message with which we're going to write. That is, it will manage success messages, error messages, and warning messages.
To do this, we'll define the class, introduce a single property, and then we'll instantiate it in the constructor. Check out this code, and I'll explain a bit more below:
<?php class Settings_Message { private $messages; public function __construct() { $this->messages = array( 'success' => array(), 'error' => array(), 'warning' => array(), ); } }
Notice that we've created a private attribute, $messages
. When the class is instantiated, we create a multidimensional array. Each index, identified either by success
, error
, or warning
, refers to its own array in which we'll be storing the corresponding messages.
Next, we need to be able to add a message, get a message, and get all of the messages. I'll discuss each of these in more detail momentarily.
Adding Messages
First, let's look at how we're adding messages:
<?php public function add_message( $type, $message ) { $message = sanitize_text_field( $message ); if ( in_array( $message, $this->messages[ $type ] ) ) { return; } array_push( $this->messages[ $type ], $message ); }
This message first takes the incoming string and sanitizes the data. Then it checks to see if it already exists in the success messages. If so, it simply returns. After all, we don't want duplicate messages.
Otherwise, it adds the message to the collection.
Getting Messages
Retrieving messages comes in two forms:
- rendering individual messages by type
- rendering the messages in the display of the administration page (complete with HTML sanitization, etc.)
Remember, there are times where we may only want to display warning messages. Other times, we may want to display all of the messages. Since there are two ways of doing this, we can leverage one and then take advantage of it in other another function.
Sound confusing? Hang with me and I'll explain all of it. The first part we're going to focus on is how to render messages by type (think success, error, or warning). Here's the code for doing that (and it should look familiar):
<?php public function get_messages( $type ) { if ( empty( $this->messages[ $type ] ) ) { return; } $html = "<div class='notice notice-$type is-dismissible'>"; $html .= '<ul>'; foreach ( $this->messages[ $type ] as $message ) { $html .= "<li>$message</li>"; } $html .= '</ul>'; $html .= '</div><!-- .notice-$type -->'; $allowed_html = array( 'div' => array( 'class' => array(), ), 'ul' => array(), 'li' => array(), ); echo wp_kses( $html, $allowed_html ); }
Notice here that we're using much of the same code from the previous tutorial; however, we've generalized it so that it looks at the incoming $type
and dynamically applies it to the markup.
This allows us to have a single function for rendering our messages. This isn't all, though. What about the times we want to get all messages? This could be to render on a page or to grab them programmatically for some other processing.
To do this, we can introduce another function:
<?php public function get_all_messages() { foreach ( $this->messages as $type => $message ) { $this->get_messages( $type ); } }
This message should be easy enough to understand. It simply loops through all of the messages we have in our collection and calls the get_messages
function we outlined above.
It still renders them all together (which we'll see one use of them in our implementation of a custom hook momentarily). If you wanted to use them for another purpose, you could append the result into a string and return it to the caller, or perform some other programmatic function.
This is but one implementation.
3. The Settings Messenger
That does it for the actual Settings_Message
class. But how do we communicate with it? Sure, we can talk to it directly, but if there's an intermediate class, we have some control over what's returned to us without adding more responsibility to the Settings_Message
class, right?
Enter the Settings_Messenger
. This class is responsible for allows us to read and write settings messages. I think a case could be made that you could split this up into two classes by its responsibility because it both reads and writes but, like a messenger who sends and receives, that's the purpose of this class.
The initial setup of the class is straightforward.
- The constructor creates an instance of the
Settings_Message
class that we can use to send and receive messages. - It associates a method with our custom
tutsplus_settings_messages
hook we defined in a previous tutorial.
Take a look at the first couple of methods:
<?php class Settings_Messenger { private $message; public function __construct() { $this->message = new Settings_Message(); } public function init() { add_action( 'tutsplus_settings_messages', array( $this, 'get_all_messages' ) ); } }
Remember from earlier in this tutorial, we have the hook defined in our view which can be found in settings.php
. For the sake of completeness, it's listed here:
<div class="wrap"> <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> <?php do_action( 'tutsplus_settings_messages' ); ?> <p class="description"> We aren't actually going to display options on this page. Instead, we're going to use this page to demonstration how to hook into our custom messenger. </p><!-- .description --> </div><!-- .wrap -->
Notice, however, that this particular hook takes advantage of the get_all_messages
method we'll review in a moment. It doesn't have to use this method. Instead, it could be used to simply render success messages or any other methods that you want to use.
Adding Messages
Creating the functions to add messages is simple as these functions require a type and the message itself. Remember, the Settings_Message
takes care of sanitizing the information so we can simply pass in the incoming messages.
See below where we're adding success, warning, and error messages:
<?php public function add_success_message( $message ) { $this->add_message( 'success', $message ); } public function add_warning_message( $message ) { $this->add_message( 'warning', $message ); } public function add_error_message( $message ) { $this->add_message( 'error', $message ); }
It's easy, isn't it?
Getting Messages
Retrieving messages isn't much different except we just need to provide the type of messages we want to retrieve:
<?php public function get_success_messages() { echo $this->get_messages( 'success' ); } public function get_warning_messages() { echo $this->get_messages( 'warning' ); } public function get_error_messages() { echo $this->get_messages( 'error' ); }
Done and done, right?
But Did You Catch That?
Notice that the messages above all refer to two other methods we haven't actually covered yet. These are private messages that help us simplify the calls above.
Check out the following private methods both responsible for adding and retrieving messages straight from the Settings_Message
instance maintained on the messenger object:
<?php private function add_message( $type, $message ) { $this->message->add_message( $type, $message ); } private function get_messages( $type ) { return $this->message->get_messages( $type ); }
And that wraps up the new Settings_Messenger
class. All of this is much simpler, isn't it?
Starting the Plugin
It does raise the question, though: How do we start the plugin now that we've had all of these changes?
See the entire function below:
<?php add_action( 'plugins_loaded', 'tutsplus_custom_messaging_start' ); /** * Starts the plugin. * * @since 1.0.0 */ function tutsplus_custom_messaging_start() { $plugin = new Submenu( new Submenu_Page() ); $plugin->init(); $messenger = new Settings_Messenger(); $messenger->init(); $messenger->add_success_message( 'Nice shot kid, that was one in a million!' ); $messenger->add_warning_message( 'Do not go gently into that good night.' ); $messenger->add_error_message( 'Danger Will Robinson.' ); }
And that's it.
A few points to note:
- If you don't call init on the
Settings_Messenger
, then you don't have to worry about displaying any messages in on your settings page. - The code adds messages to the
Settings_Messenger
, but it doesn't actually retrieve any because I am using the init method. - If you want to retrieve the messages then you can use the methods we've outlined above.
That's all for the refactoring. This won't work exactly out of the box as there is still some code needed to load all of the PHP files required to get the plugin working; however, the code above focuses on the refactoring which is the point of this entire tutorial.
Conclusion
For a full working version of this tutorial and complete source code that does work out of the box, please download the source code attached to this post on the right sidebar.
I hope that over the course of this material you picked up a number of new skills and ways to approach WordPress development. When looking over the series, we've covered a lot:
- custom menus
- introducing administration pages
- the various message types
- defining and leveraging custom hooks
- and refactoring object-oriented code
As usual, I'm also always happy to answer questions via the comments, and you can also check out my blog and follow me on Twitter. I usually talk all about software development within WordPress and tangential topics, as well. If you're interested in more WordPress development, don't forget to check out my previous series and tutorials, and the other WordPress material we have here on Envato Tuts+.
Resources
- Creating Custom Administration Pages with WordPress
- The WordPress Settings API
- How to Get Started With WordPress
- add_action
- do_action
- wp_kses
- sanitize_text_field
by Tom McFarlin via Envato Tuts+ Code
How to Create a Pinterest Showcase for Your Business
Looking for a new way to show off your best pins? Want to try Pinterest Showcase? Pinterest showcases allow businesses to rotate select pins at the top of their profiles. In this article, you’ll discover how to build a Pinterest showcase for your business profile. Why Build a Pinterest Showcase? Think of the Showcase feature [...]
This post How to Create a Pinterest Showcase for Your Business first appeared on .
- Your Guide to the Social Media Jungle
by Kristi Hines via
Wednesday, December 28, 2016
How The Internet Revolutionized Offline Retail - infographic
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Irfan Ahmad via Digital Information World
vDrop – jQuery Plugin for Custom Select Dropdowns
vDrop is a minimalistic and customisable jQuery plugin to replace standard HTML select dropdowns.
by via jQuery-Plugins.net RSS Feed
Nau Digital Creative Studio
We deliver experiences on mobile application and website that make life easier, help brand growing, and transform business to growth within digital world.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Mindvalley
We design learning experiences and publish teachings by the best authors in transformational education for 3 million students worldwide.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
MEDELA – regeneration center
A new creative web site for MEDELA – regeneration center created by ARM DESIGN
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Pedro Duarte
by Rob Hope via One Page Love
Uber | Where To?
by via Awwwards - Sites of the day
How to Use Instagram Live
Interested in adding live video to your Instagram stories? Looking for a guide to get you started? The latest update to the Instagram app includes Live video, a new feature that lets you connect with your Instagram audience in real time. In this article, you’ll discover how to use Instagram Live video within Instagram Stories. [...]
This post How to Use Instagram Live first appeared on .
- Your Guide to the Social Media Jungle
by Kristi Hines via
Tuesday, December 27, 2016
8 Powerful Components to Boost Content Marketing Success
[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]
by Web Desk via Digital Information World
Trionn Design
Trionn Design is one of India’s leading and trusted outsource web design & development agencies, located in Rajkot, Gujarat. Our agency creates Web Interfaces, Brands, IOS application designs, game design & custom Web, CMS & e-commerce development.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Lets Travel WordPress Theme
Let’s Travel is a responsive multi-purpose travel booking WordPress theme that is an ideal fit for a travel agency, tour operator, hotel, airline, car service, cruise and more.
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Intrusive Thoughts
Intrusive Thoughts is an educational hub for the OCD community that seeks to empower sufferers to lead healthier lives. After 20 years of living with undiagnosed OCD, founder Aaron Harvey turned to the internet for help. In his search for answers, he
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
Texas Thread Republic
Texas Inspired Graphic Tee Shirts
by csreladm via CSSREEL | CSS Website Awards | World best websites | website design awards | CSS Gallery
number-divider.js – jQuery Number Divider Plugin
number-divider.js is a jQuery plugin to divide numbers. You can divide numbers in inputs and simple HTML tags. If you wish use it in form then formatted values will be auto replaced on original values.
by via jQuery-Plugins.net RSS Feed
Books on graphic design
by Rob Hope via One Page Love
How to Better Serve Customers With Twitter Direct Messages
Looking for ways to improve customer care on Twitter? Do your customers tweet you for support? Moving customer service discussions to private Twitter Direct Message (DM) threads can help your support team quickly resolve issues. In this article, you’ll discover how to use the latest Twitter Direct Message features to improve your customer service. Why [...]
This post How to Better Serve Customers With Twitter Direct Messages first appeared on .
- Your Guide to the Social Media Jungle
by Kristi Hines via
Telekom: Magic Advent Calendar
by via Awwwards - Sites of the day