Cộng đồng chia sẻ tri thức Lib24.vn

Mastering WordPress Advanced Techniques

5cbcd530e026f439e214d84ae14e9256
Gửi bởi: Tiến Trần Hữu 2 tháng 5 2016 lúc 9:05:27 | Được cập nhật: 0 giây trước Kiểu file: PDF | Lượt xem: 576 | Lượt Download: 2 | File size: 0 Mb

Nội dung tài liệu

Tải xuống
Link tài liệu:
Tải xuống

Các tài liệu liên quan


Có thể bạn quan tâm


Thông tin tài liệu

Hosting unlimited: https://www.ipage.com/ Imprint Published in November 2011 Smashing Media GmbH, Freiburg, Germany Cover Design: Ricardo Gimenes Editing: Andrew Rogerson, Talita Telma Proofreading: Andrew Lobo, Iris Ljesnjanin Reviewing: Jeff Starr Idea and Concept: Sven Lennartz, Vitaly Friedman Founded in September 2006, Smashing Magazine delivers useful and innovative information to Web designers and developers. Smashing Magazine is well-respected international online publication for professional Web designers and developers. Our main goal is to support the Web design community with useful and valuable articles and resources, written and created by experienced designers and developers. ISBN: 9783943075182 Version: December 16, 2011Hosting unlimited: https://www.ipage.com/ Table of Contents Preface !e Definitive Guide To WordPress Hooks Custom Fields Hacks For WordPress Power Tips For WordPress Template Developers Advanced Power Tips For WordPress Template Developers Advanced Power Tips for WordPress Template Developers: Reloaded Ten !ings Every WordPress Plugin Developer Should Know Create Perfect Emails For Your WordPress Website Writing WordPress Guides for the Advanced Beginner Advanced Layout Templates =n WordPress’ Content Editor !e Authors Preface WordPress has many facets to show for those who like to explore its possibilities. Well known as free source blog-publishing platform, WordPress also gained popularity because of its flexibility and development options through plugins, hooks and custom fields just to name few. If you are already familiar with the fundamentals of WordPress, you might be searching for specific knowledge in order to become better expert. This Smashing eBook #11: Mastering WordPress, which is packed with exclusive advanced techniques, is probably what you are looking for. After reading thisHosting unlimited: https://www.ipage.com/ eBook, you will be able to implement various hints which WordPress has to offer for your upcoming projects. This eBook contains articles which brings topics that will help you work with custom fields, understand coding with hooks, maintain plugins, create emails for your website, write WordPress guides and administrate layout content editor templates. Three whole chapters are destined to advise you with power tips for WordPress template developers. For instance, you will get tips which address common CMS implementation challenges without plugin dependence and cover handful of API calls and integration with PHP codes. Also, these tips explain how to customize basic content administration and add features to the post and page editor in WordPress. The articles have been published on Smashing Magazine in 2010 and 2011, and they have been carefully edited and prepared for this eBook. We hope that you will find this eBook useful and valuable. We are looking forward to your feedback on Twitter or via our contact form. Andrew Rogerson, Smashing eBook Editor !e Definitive Guide To WordPress Hooks Daniel Pataki =f you’re into WordPress development, you can’t ignore hooks for long before you have to delve into them head on. Modifying WordPress core /files is big no-no, so whenever you want to change existing functionality or create new functionality, you will have to turn to hooks.Hosting unlimited: https://www.ipage.com/ In this chapter, would like to dispel some of the confusion around hooks, because not only are they the way to code in WordPress, but they also teach us great design pattern for development in general. Explaining this in depth will take bit of time, but bear with me: by the end, you’ll be able to jumble hooks around like pro. Why Hooks Exist think the most important step in grasping hooks is to understand the need for them. Let’s create version of WordPress function that already exists, and then evolve it bit using the “hooks mindset.” function get_excerpt($text, $length 150) $excerpt substr($text,$length) return $excerpt; }Hosting unlimited: https://www.ipage.com/ This function takes two parameters: string and the length at which we want to cut it. What happens if the user wants 200-character excerpt instead of 150-character one? They just modify the parameter when they use the function. No problem there. If you use this function lot, you will notice that the parameter for the text is usually the post’s content, and that you usually use 200 characters instead of the default 150. Wouldn’t it be nice if you could set up new defaults, so that you didn’t have to add the same parameters over and over again? Also, what happens if you want to add some more custom text to the end of the excerpt? These are the kinds of problems that hooks solve. Let’s take quick look at how. function get_excerpt($text, $length 150) $length apply_filters(\"excerpt_length\", $length); $excerpt substr($text,$length) return $excerpt; As you can see, the default excerpt length is still 150, but we’ve also applied some filters to it. filter allows you to write function that modifies the value of something in this case, the excerpt’s length. The name (or tag) of this filter is excerpt_length, and if no functions are attached to it, then its value will remain 150. Let’s see how we can now use this to modify the default value.Hosting unlimited: https://www.ipage.com/ function get_excerpt($text, $length 150) $length apply_filters(\"excerpt_length\"); $excerpt substr($text,$length) return $excerpt; function modify_excerpt_length() return 200; add_filter(\"excerpt_length\", \"modify_excerpt_length\"); First, we have defined function that does nothing but return number. At this point, nothing is using the function, so let’s tell WordPress that we want to hook this into the excerpt_length filter. We’ve successfully changed the default excerpt length in WordPress, without touching the original function and without even having to write custom excerpt function. This will be extremely useful, because if you always want excerpts that are 200 characters long, just add this as filter and then you won’t have to specify it every time. Suppose you want to tack on some more text, like “Read on,” to the end of the excerpt. We could modify our original function to work with hook and then tie function to that hook, like so:Hosting unlimited: https://www.ipage.com/ function get_excerpt($text, $length 150) $length apply_filters(\"excerpt_length\"); $excerpt substr($text,$length) return apply_filters(\"excerpt_content\", $excerpt); function modify_excerpt_content($excerpt) return $excerpt "Read on…"; add_filter(\"excerpt_content\", \"modify_excerpt_content\"); This hook is placed at the end of the function and allows us to modify its end result. This time, we’ve also passed the output that the function would normally produce as parameter to our hook. The function that we tie to this hook will receive this parameter. All we are doing in our function is taking the original contents of $excerpt and appending our “Read on” text to the end. But if we choose, we could also return the text “Click the title to read this article,” which would replace the whole excerpt. While our example is bit redundant, since WordPress already has better function, hopefully you’ve gotten to grips with the thinking behind hooks. Let’s look more in depth at what goes on with filters, actions, priorities, arguments and the other yummy options available. Filters And Actions Filters and actions are two types of hooks. As you saw in the previous section, filter modifies the value of something. An action, rather than modifying something, calls another function to run beside it.Hosting unlimited: https://www.ipage.com/ commonly used action hook is wp_head. Let’s see how this works. You may have noticed function at the bottom of your website’s head section named wp_head(). Diving into the code of this function, you can see that it contains call to do_action(). This is similar to apply_filters(); it means to run all of the functions that are tied to the wp_head tag. Let’s put copyright meta tag on top of each post’s page to test how this works. add_action(\"wp_head\", \"my_copyright_meta\"); function my_copyright_meta() if(is_singular()){ echo \"\"; !e Workflow Of Using Hooks While hooks are better documented nowadays, they have been neglected bit until recently, understandably so. You can find some good pointers in the Codex, but the best thing to use is Adam Brown’s hook reference, and/or look at the source code. Say you want to add functionality to your blog that notifies authors when their work is published. To do this, you would need to do something when post is published. So, let’s try to find hook related to publishing. Can we tell whether we need an action or filter? Sure we can! When post is published, do we want to modify its data or do completely separate action? The answer is the latter, so we’ll need an action. Let’s go to the action reference on Adam Brown’s website, and search for “Publish.” The first thing you’ll find is app_publish_post. Sounds good; let’s click on it. The details page doesn’t give us lot of info (sometimes it does), so click on the “View hook in source” link next to your version of WordPress (preferably the mostHosting unlimited: https://www.ipage.com/ recent version) in the table. This website shows only snippet of the file, and unfortunately the beginning of the documentation is cut off, so it’s difficult to tell if this is what we need. Click on “View complete file in SVN” to go to the complete file so that we can search for our hook. In the file am viewing, the hook can be found in the _publish_post_hook() function, which according to the documentation above it is “hook to schedule pings and enclosures when post is published,” so this is not really what we need. With some more research in the action list, you’ll find the publish_post hook, and this is what we need. The first thing to do is write the function that sends your email. This function will receive the post’s =D as an argument, so you can use that to pull some information into the email. The second task is to hook this function into the action. Look at the finished code below for the details. function authorNotification($post_id) global $wpdb; $post get_post($post_id); $author get_userdata($post->post_author); $message \" Hi \".$author->display_name.\", Your post, \".$post->post_title.\" has just been published. Well done! \"; wp_mail($author->user_email, \"Your article is online\", $message); add_action(\'publish_post\', \'authorNotification\'); Notice that the function we wrote is usable in its own right. It has very specific function, but it isn’t only usable together with hooks; you could use it in yourTrên đây chỉ là phần trích dẫn 10 trang đầu của tài liệu và có thế hiển thị lỗi font, bạn muốn xem đầyđủ tài liệu gốc thì ấn vào nút Tải về phía dưới.