segunda-feira, 15 de dezembro de 2014

How To Add Holiday Notices To Your WordPress Site

The festivities are on us and what improved approach to widespread a hearten than putting adult a holiday-themed notice on your site? You competence be going on vacation soon, we competence wish to hail your visitors with a special card, or we competence usually wish to arrangement a special message.


There are lots of WordPress Christmas plugins out there we’ve already looked at, yet how about doing it on your own? Do we have a bit of time for a brief WordPress plan over a holidays? Follow along to learn how to supplement some Christmas notices to your website around calm widget, gummy bar and a opposite widget.



Creating A Text Widget


There’s no rocket scholarship here, sorry! You can supplement a discerning summary and arrangement it in a widget area by going to Appearance Widgets, tedious a calm widget into a sidebar and entering your summary in a box. You can use HTML in there, so we could piquancy things adult with an picture if we like.


I agree, this is a bit tedious yet it could be usually a right volume of festiveness to add. Many some-more critical sites such as e-Commerce sites or personal portfolios would be impacted negatively by dancing Santas yet people still conclude a small holiday hearten nonetheless.


Sticky Bar


How about a hang bar, identical to a Quick Notice plugin yet one we coded ourselves? We can also make it hang to a tip of a page on corkscrew easily.


The initial step is formulating a plugin, let me uncover we how:


In your plugins directory, emanate a new folder named “my-sticky-bar”. Inside that folder emanate a record named “my-sticky-bar.php” and pulp a following calm into it:


?php /**
* Plugin Name: My Sticky Bar
* Plugin URI: http://yourwebsite.com
* Description: My Christmas Project - formulating a gummy bar
* Version: 1.0.0
* Author: Your Name
*/

Once we save this record we should be means to activate a plugin around a Plugins territory in a WordPress admin. It doesn’t do anything nonetheless yet we’ll shortly repair that!


We need to do dual things: use PHP and HTML to output a summary and use CSS to style it.


In a “my-sticky-bar.php” record use a following formula to supplement a required HTML in a scold place:


add_action( 'wp_footer', 'my_sticky_bar_display' );
function my_sticky_bar_display()
echo "div id='my-sticky-bar'Merry Christmas Everyone!/div";

The initial line is a WordPress hook, it tells WordPress to govern a “my_sticky_bar_display” duty usually before a finish of a body. It’s excellent to bucket a HTML for a bar during a finish of a page. It isn’t that critical and can be repositioned around CSS.


At this theatre we should be means to see your summary during a bottom of your site, though any formatting. To supplement formatting we’ll have to insert a stylesheet by enqueueing it.


In a same file:


add_action( 'wp_enqueue_scripts', 'my_sticky_bar_styles' );
function my_sticky_bar_styles()
wp_enqueue_style( 'my-sticky-bar', plugin_dir_url( __FILE__ ) . '/my-sticky-bar.css');

Enqueueing is a approach of adding scripts and styles modularly to WordPress. No need to know all that yet — if we pulp this into a plugin record and emanate a “my-sticky-bar.css” record we can start adding your styles. Let’s do that now.


#my-sticky-bar 
position: absolute;
top: 0px;
width: 100%;
text-align: center;
color:
background:
padding: 11px 0;
text-transform: uppercase;
letter-spacing: 2px;

Best of all, if we reinstate “absolute” with “fixed” a summary will hang to a tip of a window and will follow users around when they scroll.



This process is good since we have full control over everything! For example, how about gripping this plugin active all a time yet usually arrangement a summary on a 25th of December? No problem, usually cgange a my_sticky_bar_display duty slightly.


add_action( 'wp_footer', 'my_sticky_bar_display' );
function my_sticky_bar_display()
if( date( 'd' ) == 25 date( 'm' ) == 12 )
echo "div id='my-sticky-bar'Merry Christmas Everyone!/div";


Counter Widget


We’ve already combined some elementary calm into a calm widget, yet what about formulating a countdown for a users?


The initial step is to let WordPress know that we wish to emanate a tradition widget. We need to conclude a duty and offshoot it to widgets_init. Create a new plugin, usually like before, and in a categorical file, pulp a following:



add_action( 'widgets_init', 'christmas_countdown_widget' );
function christmas_countdown_widget()
register_widget( 'Christmas_Countdown_Widget' );

We will need to emanate a category named Christmas_Countdown_Widget with a few functions within it. Let’s start by formulating a constructor function. Paste all next into a categorical plugin record as well.



class Christmas_Countdown_Widget extends WP_Widget

duty __construct()
parent::__construct(
'christmas_countdown_widget',
'Christmas Countdown Widget',
array( 'description' = 'A elementary Christmas countdown' )
);



The constructor duty contains some critical information that is used to build a widget. It contains a id of a widget (christmas_countdown_widget), a name that will apear in a backend UI (Christmas Countdown Widget) and a outline that is also shown.


We can supplement a duty named form() that can be used to outlay some settings for a widget. We won’t be regulating it here, yet keep it in mind for destiny projects. I’ll usually supplement an dull duty for now.


We will use a widget() duty to outlay a widget to a front end. Let’s supplement a elementary countdown now. Paste a following formula inside a class, next a __construct() function. Note that I’m also adding a dull form() duty now.



function form()

function widget()
relate "Time to Christmas: " . human_time_diff( time(), mktime( 0, 0, 0, 12, 25, date( 'Y' ) ) );

We use a human_time_diff() duty offering by WordPress to figure out a time between currently and Christmas day. This will arrangement something like: “Time to Christmas: 5 days”.


This is flattering basic, yet we can use any arrange of images, text, styles and Javascript to make this demeanour awesome. The final and finish formula for a widget is as follows:



add_action( 'widgets_init', 'christmas_countdown_widget' );
function christmas_countdown_widget()
register_widget( 'Christmas_Countdown_Widget' );


class Christmas_Countdown_Widget extends WP_Widget

duty __construct()
parent::__construct(
'christmas_countdown_widget',
'Christmas Countdown Widget',
array( 'description' = 'A elementary Christmas countdown' )
);


duty form()

duty widget()
relate "Time to Christmas: " . human_time_diff( time(), mktime( 0, 0, 0, 12, 25, date( 'Y' ) ) );




Happy Holidays!


I wish I’ve given we a few ideas to supplement some Christmas hearten to your website! If we don’t applaud Christmas we can always use these methods and request them to your possess culture.


Eating and celebrating all via a holidays? Be certain to review a essay on Christmas WordPress Plugins for tons of good ideas and discerning entrance to good holiday stuff!




How To Add Holiday Notices To Your WordPress Site

Nenhum comentário:

Postar um comentário