Library for WordPress. How do I faylohranilische
I decided to remake the page with the books on its website, taking advantage of access to MySQL, which is still used in wordpress. The essence of the alteration is quite simple - to create a function that, in finding the body of the page specific tag, it would be a substitute for certain information from the database wordpress. The database, in turn, can be anything, in my case - descriptions and links to the files of books presented in the "Library". Complicated to write such a function is nothing, on this principle by the way work many plugins, such as embed videos or music on the page.
The principle of operation of the function insert data to a page
If you call it in the loop traverses the list of attributes of the tag, then for each attribute queries to the database MySQL, the resulting sample parses and outputs the data to a page in a table. The data are entered into the database with hands, but want to, using the guide " How to write a plugin for WordPress ", can make a page to download the information into the database wordpress through a browser.
Create a new table in the WordPress database
Prepare the field for follow-up - create a MySQL-based WordPress new table (I named my biblio) the following:
| id | section | isbn | name | authors | description | book_url | book_cover_url |
The easiest way to create a table - this query CREATE TABLE to the database:
CREATE TABLE `wp_biblio` ( `Id` INT NOT NULL AUTO_INCREMENT, `Section` VARCHAR (30) NOT NULL default'', `Isbn` VARCHAR (70) NOT NULL default'', `Name` VARCHAR (100) NOT NULL, `Authors` VARCHAR (50) NOT NULL default'', `Description` TEXT NOT NULL default'', `Book_url` VARCHAR (255) NOT NULL default'', `Book_cover_url` VARCHAR (255) NOT NULL default'', UNIQUE KEY id (id) ) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
For those who want to better understand the request parameters in the " Library "presented a great book SQL. The Complete Guide (2nd edition).
WordPress-function output to the page
Now is the time to do directly by writing a function to wordpress, which will receive data from the database and display them on the page in the specified location. I'm showing where to insert the data tag is the type of
A | [Biblio = "attr1, ATTR2, AttrN"] |
. Tag attributes (words in quotation marks) will provide the names of sections of the library (formatting attributes are stored, ie, if you specify a perl and php for example, the section of the library and will be named in lower case) and they also serve to separate the categories of books when asked to the database.
function my_biblio_former ($ content) { global $ wpdb; $ Biblio_table = $ wpdb-> prefix. "Biblio"; # set the descriptor table with books $ Pattern = "/ \ [biblio = \ "(.+?) \" \] / is "; # parse the template tag [biblio =" atribut1, atributN "] if (preg_match ($ pattern, $ content, $ book_sect) & & is_page ('Library')) {# every time a tag is found on that page - it is handled $ Sections = preg_split ("/ [\ s ,]+/", $ book_sect ['1 ']); # formed an array of tag attributes $ New_content = '<table width="100%" border="1" frame="void" rules="rows" cellspacing="0" cellpadding="2">'; foreach ($ sections as $ b_section) {# start processing the array tag attributes $ Sql = "SELECT * FROM $ biblio_table WHERE` section `= '$ b_section'"; # sample from the database for the current attribute $ Books = $ wpdb-> get_results ($ sql, ARRAY_A); # get the results of the sample in the form of hash $ New_content .= '<tr align="center"> <td> <h2>'. $ B_section. '</ H2> </ td> </ tr>'; foreach ($ books as $ book) {# processing the sample results from the database $ New_content .= '<tr align="center">'; # for each book on a table a new row $ New_content .= '<td align="center"> <img src = "'. $ Book ['book_cover_url']. '" Alt = "'. $ Book ['name']. ' download "> </ td> '; # in the first column is the book cover $ New_content .= '<td align="center"> <strong> <a href="'. $book['book_url'] .'">'. $ Book ['name']. '</ A> </ strong> <br /> <strong> ISBN: </ strong>'. $ Book ['isbn']. '<br /> <em>'. $ Book ['authors']. '</ Em> <br />'. $ Book ['description']. '</ Td>'; # in the second column - ISBN, author (s), description of $ New_content .= '</ tr>'; End} # sample processing End} # attribute processing $ New_content .= '</ table>'; $ Content = preg_replace ($ pattern, $ new_content, $ content); return $ content; #} End of processing the tag [biblio]
The function code, I tried to adequately comment on the detail and queries to a MySQL database from WordPress, I again told her to write a guide for WordPress plugin (see part five - Writing a plugin for WordPress. functional component. ).
Write a function to add to the functions.php file its theme wordpress (for example, using the integrated editor of the administrative panel: Appearance -> Editor).
Do not forget to also install a filter (the filter I told earlier ) to call fnktsii:
A | add_filter ('the_content', 'my_biblio_former', 1); |
Conclusion - to check the work function of the output data from the database
That's all there ready to function and work, as exemplified by page " library "of my blog, representing a single tag
A | [Biblio = "Perl, PHP, SQL"] |
, With a section of the library are displayed in the order in which they were listed in the attributes of the tag.
Discussed in this article function is easy to adapt to the organization in WordPress-blog file storage with a convenient display of data on them (binary data can be stored directly in the database if you wish), or some similar problems.
Experiment and create good luck! ![]()
More on similar topics:
Filed under: Blogging , Coding |
Tags: How-to , PHP , SQL , WordPress , blog , programming |
No Comments 

Recent comments