WordPress plug-in Building Part 4: viewing content displayed in short code

Welcome to the fourth part of the plug-in building series! So far, we have used WordPress plugin boilerplate, set ourselves in consideration of the structure, registered our own custom post types, and created a separate place for the back-end storage of content. This will provide an option for plug-in users to actually display the quotation marks of the input system. Let’s take a look at how to build several functions that handle random selection of quotation marks, get started, and then create your own shortcut code options. At the end of this installment, the administrator should be able to enter content into your mind and easily display it on the front end in a way familiar to many other plug-ins. The
Let’s start by accessing data of interest. You have encountered the problem of saving citations using quote access custom post type, so the first thing to deal with is to return results only from this dataset. A quick tour of the relevant sections of the plug-in manual provides a good overview of how to perform and some convenient sample code$ Args = array (‘post\u type’=>’product’,’posts\u per\u page’=> 10);
$loop = new wp_query ($args);
While ($loop->have_posts()): $loop->the_post();
Title ();
Echo ‘male’;
Content ();
Echo \
During this time As shown above, wp\u query() is used to get the results and pass arguments that define the specific post type we track and the number of results per page to return. So far, it seems cool in theory, but there are two immediate problems. The
How to quickly test operations? Where to insert WordPress plugin boilerplate in the settings? One question at a time Just as the testing wp_query() document and the mastering wp_query series strongly recommended by Rachel mccollin point out whether the database can return any reference, we must run it inside the loop for testing. Since there is no exciting content in the current local test site, we will insert some navigation code into the home page template of the basic topic to quickly determine whether they work. For us, \/themes\/twentysixteen\/page. In PHP. Our custom post type was previously defined with RDM quote, so what we actually do is search for posts and fix them in the screen list. The
$args= array(
\
);
\/\/Custom query.
$query= new wp_query ($args);
\/\/Verify that query results exist.
If ($query->have\u posts()){
Echo \
\/\/Start a cycle of query results.
Period ($query->have\u posts(){
$query->the_post();
? >
Male
Male \
Male
Male
Male
Male ‘;
}The
\/\/Restore the original post data.
Wp\u reset\u postdata(); The administrator has entered a lot of quotation marks in the custom section. The
Based on the above code, we expect to see the title listed by the link at the bottom of all pages viewed in the front end. http:\/\/www.randomquotes.dev\/sample-page Let’s quickly go to the sample page provided with WordPress. It seems that we are doing business potentially! Now, we want to make sure that we can limit the number of quotation marks displayed and that the display period is random. Old code
Add\u shortcode (\
}\/\/As you can see from the register_shortcodes() document, by default, WordPress contains the name of the shortcut code and the specific functions used to run the shortcut code. Now go ahead Let’s display the short code on the screen, and finally fill in the list_quotes() function to get some results on the page. The
\/**The
*Process shortcut code randomquote
*The
*@Param array$atts properties of shortcut code
*The
*The
*@Return mixed$output buffer output
*\/The
Public function list_quotes ($atts=array(){
Ob_start();
$args=shortcode\u ATTS (array(
\
\
$atts
);
$items = $this->get\u rdm\u quotes ($args[‘num-quotes’]);
\/\/Var_dump ($items);
If (is_array ($items) | is_object ($items)){
Echo (‘male’. $args[‘quotes-title’]. ‘male’);
Foreach (convert $items to $item){
Echo (‘male’. $item->post\u title. ‘|male’. $item->post\u content. ‘male’);
}\/\/Each
Eco (\
}Another{
Eco$project;
}The
$output =ob\u get\u contents();
Obuend_clean();
Return $output;
}\/\/Get\u rdm\u quotes() won’t win the code quality award here, but there are checkmarks in the main boxes above. You can search for quick code attributes through the shortcode_atts() function, and set default values for the number of Posts and titles. If you need to quickly check the actually processed data, you will leave behind a fast and dirty var_dump() call. Associate the data with the default HTML layout, display it on the page, and use the output buffer to prevent potential display problems on the front end. There is certainly room for improvement here, but let’s see how many results we can get first. We will start by invoking the default shortcut code from the standard post. When viewing the real-time version of the post, you can see that the default value works very well. Now add a part of our inherent attributes to make sure it works properly. If you quickly refresh the page, you will find that the properties work normally. There are many things that can be sorted out here, but we will start from this point of view before the details become too complicated. In the next lesson, we will quickly View HTML content in a more organized way to make it look ugly. I repeat, if you are interested in exploring more specific versions of some of the concepts introduced above, it is best to build on the more complete plug-in of Chris Wilcoxon in GitHub. This time. Conclusion we have closed the important loop in this series, and the results of our efforts can be seen on the screen. In this step, the smallest executable plug-in is running. You can enter custom content through the back end and use shortcut codes with parameters to display content as needed throughout the site. As in the previous step, we adopted a frameless approach, leaving a lot of room for improvement, but we have come a long way since the series began. The next section will clear some loose threads for this time period and create some user settings for the plug-in on the back end. As usual, we are waiting for comments and questions. So far, through a series of articles, there are questions about our journey

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *