Useful tips to get commenced wthe itemh WordPress hooks


Useful tips to get commenced wthe itemh WordPress Hooks

Advertis usuallying

Even if the hooks in WordPress amazing as well as everyone uses them, consciously or unconsciously, I get the impression, Some advanced users, developers, as well as especially the front still seems to escape them. If you feel like you're holding back on the hook, too, which this usually article will help you get commenced. I'm also going reveal some interesting details to those who think which they are quthe iteme familiar wthe itemh the hooks

Do you want to read this usually article, especially if you want :.

  • understas well as code fragments wthe itemh hooks such as those inside the forums;
  • expas well as WordPress, plugins as well as themes, not breaking updates;
  • learn how to avoid common problems ;
  • allow addthe itemionals to expas well as their code

My examples are taken by the core of WordPress "as well as by my work as a freelance designer as well as the author of the plugin Extended classified ..

Hooks in WordPress
hooks in WordPress useful as well as widely used, however not easy. But you can expas well as the plugins as well as themes, not breaking them, as well as avoid the common problem of service on the road.

What Hooks? Link

Whenever you want to do something or set up something in WordPress, the item is usually very likely , the hook can be used.

code "to do" Hook is usually called the "action". Wherever the action is usually determined, you can do your own code. Here are some examples:

  • to send a letter to the author as soon as the post was publis usuallyhed,
  • to download the file the user script inside the footnote of the page;
  • Add instructions above entry.

The most important action is usually to hook posthe itemion. There are action hooks all core WordPress, which allow to do their job.

code "tune" Hook called "filter". The filter allows you to change or adjust the value as well as return the item in a brin addition to-new form. Some examples:

  • capthe itemalization job tthe itemle in
  • is usually a reference to the relevant office after the main content of
  • change option, which is usually extracted by the database.

not only important for the posthe itemion of the filter, however is usually given as well as returned values ​​of business, too. As wthe itemh actions, you can assume which WordPress features a filter for almost every value the item processes .

Regular hook elements Link

connect only one posthe itemion. Nothing happens here default. To make the item work, you need at least three basic functions, which, in combination, I like to call "normal hook."

Let's use wp_head hook as an example for action as well as the_content filter.

Hook (noun) Link

hook the itemself when as well as where the magic happens. Imagine the item as a hook to put the climber inside the mountain face. It features a certain posthe itemion, as well as if they or addthe itemional climbers want to move forward, they can use the item

Action hooks are placed wthe itemh do_action () , as well as the following :.

  do_action ('wp_head');  

Filter hooks used apply_filters () , as follows:

  $ content = apply_filters ('the_content', $ content) ;.  

As you can see, we have to catch the output of the filter also

Action Filters Link

The next element inside the routine of the hook is usually action or filter . This kind of usually is usually at least one function which you specify, to do something or filter. That might be the actual climber who are willing to use any hook to get a lthe itemtle higher.

The action is usually triggered wp_head is usually NOINDEX () .

  function NOINDEX () {// If the blog is usually not public, tell robots to go. if ('0' == get_option ('blog_public')) wp_no_robots (); }  

This kind of usually simply checks to see if you dis usuallyabled for search engine vis usuallyibilthe itemy. If so, wp_no_robots () adds the robots meta tag, which tell search engines not to index your sthe iteme.

Example filter the_content is usually wpautop () . He is usually responsible for wrapping your paragraphs in a

as well as wthe itemh
for carriage return. An important part of the plugin is usually as follows:

  function wpautop ($ pee, $ w = TRUE) {// ... return $ pee; }  

Other than the effect of the filter function needs at least one argument. This kind of usually argument must be returned later.

Hook (verb) Link

While our real life mountain climber kat this points what they can do wthe itemh a hook in WordPress daily routine Hook we have I might tell them which this usually is usually a very special hook for their use . This kind of usually means which we have to connect (verb) our function inside the hook (noun).

This kind of usually is usually done by a function which is usually also often incorrectly called "hook" (noun). When I explainside the procedure of the hook, the item is usually expressed as a verb, however if I need to use a noun, then I might call the item a "connection."

to wp_head hook, as well as NOINDEX () action, the connection is usually made wthe itemh this usually line:

  add_action ('wp_head', 'NOINDEX ', 1);  

The third parameter is usually a priorthe itemy, which we shall consider below

Connection wpautop () in the_content done wthe itemh this usually line :.

  add_filter ('the_content', 'wpautop');  

These basics hook routine. We'll look at a few of them inside the following sections.

You are already using these traps Link

Actions as well as filter are hidden deep inside the kernel, however you are just interested in changing the subject, so why should you mess wthe itemh them?

It is usually true which, even if you're "just" theme code or sometimes put something in your theme functions.php , you probably already use actions as well as filters.

Let's look at Head section Twenty Fifteen theme which can be found in WP-content / themes / twentyfifteen / header.php .

    class = "no-JS">          

There truly is usually not much between the starting as well as closing tags , so take a look at the source code inside the browser. You'll see all kinds of metadata elements, including tag.

The elements which may be mis usuallysing in [1945040headerphp] file will be added to wp_head () function. If you have an IDE wthe itemh an open (ie, software code you wrthe iteme), as well as the item allows you to go to the definthe itemion of the function, then you should truly use which at this point as well as check the contents of wp_head () .

  / ** * Fire wp_head action * * * since 1.2.0 / function wp_head () {/ ** * Print scripts or data inside the tag of the head at the front end. * * * Since 1.5.0 / do_action ('wp_head'); }  

If you were to remove the comments, you'll see which the only thing wp_head () does by placing [1945040wp_head] hook.

In practice, this usually means which your subject can also just use do_action ('wp_head'); , instead of wp_head () ;.

This kind of usually has well asler uses the kernel already. Some compounds which defines a default WordPress

  add_action ('wp_head', '_ wp_render_tthe itemle_tag', 1); add_action ('wp_head', 'wp_enqueue_scripts', 1); add_action ('wp_head', '', feed_links 2); add_action ('wp_head', 'feed_links_extra', 3); add_action ('wp_head', 'rsd_link'); add_action ('wp_head', 'wlwmanifest_link'); add_action ('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); add_action ('wp_head', 'locale_stylesheet'); add_action ('wp_head', 'NOINDEX', 1); add_action ('wp_head', 'print_emoji_detection_script', 7); add_action ('wp_head', '', wp_print_styles 8); add_action ('wp_head', '', wp_print_head_scripts 9); add_action ('wp_head', 'wp_generator'); add_action ('wp_head', 'rel_canonical'); add_action ('wp_head', 'wp_shortlink_wp_head', 10, 0); add_action ('wp_head', 'wp_sthe iteme_icon ", 99);  

Among them our NOINDEX () action, as well as (wthe itemh WordPress 4.1) call for _wp_render_tthe itemle_tag ( ) , which generates name tag.

the_content Link

Example hook filter which you have used a few times, not kat this pointing which the item the_content .

This kind of usually lies behind the the_content () , which is usually used to dis usuallyplay the contents of the post or page. It also calls for the_content hook.

  function the_content ($ more_link_text = NULL, $ strip_teaser = false) {$ content = get_the_content ($ more_link_text, $ strip_teaser); // ... $ content = apply_filters ('the_content' , $ content); $ content = str_replace (']]>', ']] & GT;', $ content); echo $ content; }  

When you call the_content () in a clean install of WordPress, several filters already connected to the the_content .

  add_filter ('the_content', 'wptexturize'); add_filter ('the_content', 'convert_smilies'); add_filter ('the_content', 'convert_chars'); add_filter ('the_content', 'wpautop'); add_filter ('the_content', 'shortcode_unautop'); add_filter ('the_content', 'prepend_attachment');  

default filter inside the automobilee of the format as well as some special characters inside the text. It is usually also widely used in many plugins for fastening elements such as social sharing icons or related posts at the end of the main content inside the post.

Lessons Using the_content Link

Even if you do not consciously use this usually hook, the item's good to kat this point a lthe itemtle about the item in order to understas well as common problems. Here are two stories, which prevents mis usuallyuse of features is usually working properly.

As you can see in the_content () , there is usually also a call to get_the_content () , I saw the latter is usually used instead of the previous a couple of those. This kind of usually, however, bypasses the hook, as well as a variety of functions, including formatting the post by default, will not operate.

If you connect to the the_content , then kat this point which the item is usually is usually used not only on content pages, however the pages of the lis usuallyt , such as the archive as well as the home page . I still remember your client to me about his usually first page, which has been slow due to the common plugin which introduced social-sharing widget teaser below each post, each of which is usually called the home simultaneously. There was no "OFF" for such behavior.

Parameters hook Regular Link

The most common options which you should kat this point about when building or debugging routine hook is usually the name of the hook, the function name, priorthe itemy, as well as arguments which you give to the function.

Needless to say, the devil is usually inside the details.

For the record, is usually which the main challenge add_filter () looks, including all parameters:

  add_filter ($ tag, the function $, $ priorthe itemy, $ accepted_args);  

Naming Hooks ($ tags) Link

You can not do anything about the name of the hook, which is usually used inside the core of WordPress "or plugins or themes however keep in mind a few things when creating your own name.

The rule of thumb I follow which action should include the time of the call, as well as filters should include the time as well as the value of , which will be changed.

[1945040wp_head] as well as [1945040wp_footer] are good examples of actions, called in accordance wthe itemh regulations. It is usually also commonly used prefix or suffix, for example, to advance start as well as after as well as end of

The wp_delete_post () , which is usually obviously responsible for the removal of the post includes the following hooks:

  • before_delete_post
  • delete_post
  • deleted_post
  • after_delete_post

, when I wrthe iteme a filter, I try to turn on the filtered value, too. the_content is usually a good example of this usually.

Hook Name prefix Link

In addthe itemion, the posthe itemions as well as values, you have to give your hook prefix which identifies the item as your own. Use the following examples:

  • wpseo_ by Yoast SEO,
  • genesis usually_ by wthe itemhin Genesis usually
  • advanced_ads_ my Ad Extensions plugin.

This kind of usually serves two purposes.

First, the item is usually to prevent conflicts wthe itemh hooks in addthe itemional plugins or themes.

Second, if someone uses your hook inside the theme or plugin, which third parties are easier to understas well as where the item originates hook.

Dynamic Hook Names Link

Imagine which you have many options in your plugin as well as you want addthe itemionals to be able to use a filter on each of them. You can ethe itemher call apply_filters () for each option or use the idea of ​​a dynamic name the hook, as well as the core of WordPress is usually doing in get_options () function .

It contains the following hooks:

  • "pre_option_. $ Option
  • " default_option_. $ Option
  • "option_. $ Option

Kat this pointing this usually, you could hook into option_blogname or option_blogdescription to change the name or description of the blog dynamically.

This kind of usually may also be the reason why you can not find the hook you read the source code wthe itemhout reducing the dynamic part.

Magic "all " Link

Special all tag can be used as the name of the hook when you call add_action () as well as add_filter () . This kind of usually will cause the hooked function which will be used for each hook (both actions as well as filters).

The only time I ever needed the item for debugging the whole environment of the hook.

function Name Link

When a simple function call, add a prefix. This kind of usually prevents conflicts wthe itemh addthe itemional assis usuallytants functions which identify the item.

On the forums, you'll often find the names of functions, starting wthe itemh MY_ . Change is usually something which can later be identified as yours

  add_filter ('the_content', 'bestpluginever_capthe itemalize_all_words.');  

Connecting a static function inside the class can also be

  add_filter ('the_content ", array (' bestpluginever ',' capthe itemalize_all_words'));  

Call the item an instance of this usually class will be as follows:

  add_filter ('the_content ", array ($ bestpluginever,' capthe itemalize_all_words'));  

You can also call the method inside the case of the same class, for example:

  add_filter ('the_content ", array ($ this usually,' capthe itemalize_all_words')) ;  

Make sure which the functions of the state as well.

Use the default PHP function, too Link

Sometimes, creating the items own function no. must be

The program hook capthe itemalize the first letter of each word inside the tthe itemle of Post:

  function bestpluginever_capthe itemalize_tthe itemle ($ name) {return ucwords ($ name);} Add_filter ('the_tthe itemle' , 'bestpluginever_capthe itemalize_tthe itemle');  

because ucwords () is usually a normal function of PHP, which receives as well as returns a value as any addthe itemional filter functions, you can also cut four in this usually line:

  add_filter ('the_tthe itemle', 'ucwords');  

Priorthe itemy Link

The third parameter inside the add_action () as well as add_filter () a priorthe itemy. It establis usuallyhes the order in which functions are called several interceptions.

This kind of usually parameter is usually optional as well as defaults to 10 , unless addthe itemionalwis usuallye specified. Multiple functions wthe itemh the same priorthe itemy will be called inside the order in which they were recorded on the hook.

Parameter Priorthe itemy cost me a lot of nerves. Advanced Classifieds use the_content when adminis usuallytered before or after the ad content. As I said, many addthe itemional plug-ins to do the same for inserting content such as past or similar posthe itemions.

Specifies an ad or addthe itemional content, whether connected inside the first place. To reduce support requests on the "wrong" order of elements which, in fact, very specific definthe itemion, I ended up allowing the user to select the priorthe itemy of the filter as an option.

You decide to leave a comment. That's cool! Therefore, please do not use keyword spamming or domain as your name, addthe itemionalwis usuallye the item will be removed. Let nothing meaningful conversation instead. Thanks for dropping by!

You decide to leave a comment. That's cool! Therefore, please do not use keyword spamming or domain as your name, addthe itemionalwis usuallye the item will be removed. Let nothing meaningful conversation instead. Thanks for dropping by!
Useful tips to get commenced wthe itemh WordPress hooks : Useful tips to get commenced wthe itemh WordPress hooks
Share on Google Plus

0 Response to " Useful tips to get commenced wthe itemh WordPress hooks "

Post a Comment