Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ. Các bạn đi ngang nếu được cho Lâm 1 like và 1 đăng ký kênh Youtube nhé !!
The first thing we need to do is add the extra fields to the category edit form using the hook edit_category_form_fields and we use a simple function that will print out the extra fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<span class="pun"><?</span><span class="pln">php </span><span class="com">//add extra fields to category edit form hook</span><span class="pln"> add_action </span><span class="pun">(</span> <span class="str">'edit_category_form_fields'</span><span class="pun">,</span> <span class="str">'extra_category_fields'</span><span class="pun">);</span> <span class="com">//add extra fields to category edit form callback function</span> <span class="kwd">function</span><span class="pln"> extra_category_fields</span><span class="pun">(</span><span class="pln"> $tag </span><span class="pun">)</span> <span class="pun">{</span> <span class="com">//check for existing featured ID</span><span class="pln"> $t_id </span><span class="pun">=</span><span class="pln"> $tag</span><span class="pun">-></span><span class="pln">term_id</span><span class="pun">;</span><span class="pln"> $cat_meta </span><span class="pun">=</span><span class="pln"> get_option</span><span class="pun">(</span> <span class="str">"category_$t_id"</span><span class="pun">);</span> <span class="pun">?></span> <span class="tag"><tr</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"form-field"</span><span class="tag">></span> <span class="tag"><th</span> <span class="atn">scope</span><span class="pun">=</span><span class="atv">"row"</span> <span class="atn">valign</span><span class="pun">=</span><span class="atv">"top"</span><span class="tag">><label</span> <span class="atn">for</span><span class="pun">=</span><span class="atv">"cat_Image_url"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'Category Image Url'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></label></th></span> <span class="tag"><td></span><span class="pln"> <input type="text" name="Cat_meta[img]" id="Cat_meta[img]" size="3" style="width:60%;" value="</span><span class="pun"><?</span><span class="pln">php echo $cat_meta</span><span class="pun">[</span><span class="str">'img'</span><span class="pun">]</span> <span class="pun">?</span><span class="pln"> $cat_meta</span><span class="pun">[</span><span class="str">'img'</span><span class="pun">]</span> <span class="pun">:</span> <span class="str">''</span><span class="pun">;</span> <span class="pun">?></span><span class="pln">"></span><span class="tag"><br</span> <span class="tag">/></span> <span class="tag"><span</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"description"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'Image for category: use full url with '</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></span></span> <span class="tag"></td></span> <span class="tag"></tr></span> <span class="tag"><tr</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"form-field"</span><span class="tag">></span> <span class="tag"><th</span> <span class="atn">scope</span><span class="pun">=</span><span class="atv">"row"</span> <span class="atn">valign</span><span class="pun">=</span><span class="atv">"top"</span><span class="tag">><label</span> <span class="atn">for</span><span class="pun">=</span><span class="atv">"extra1"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'extra field'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></label></th></span> <span class="tag"><td></span><span class="pln"> <input type="text" name="Cat_meta[extra1]" id="Cat_meta[extra1]" size="25" style="width:60%;" value="</span><span class="pun"><?</span><span class="pln">php echo $cat_meta</span><span class="pun">[</span><span class="str">'extra1'</span><span class="pun">]</span> <span class="pun">?</span><span class="pln"> $cat_meta</span><span class="pun">[</span><span class="str">'extra1'</span><span class="pun">]</span> <span class="pun">:</span> <span class="str">''</span><span class="pun">;</span> <span class="pun">?></span><span class="pln">"></span><span class="tag"><br</span> <span class="tag">/></span> <span class="tag"><span</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"description"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'extra field'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></span></span> <span class="tag"></td></span> <span class="tag"></tr></span> <span class="tag"><tr</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"form-field"</span><span class="tag">></span> <span class="tag"><th</span> <span class="atn">scope</span><span class="pun">=</span><span class="atv">"row"</span> <span class="atn">valign</span><span class="pun">=</span><span class="atv">"top"</span><span class="tag">><label</span> <span class="atn">for</span><span class="pun">=</span><span class="atv">"extra2"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'extra field'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></label></th></span> <span class="tag"><td></span><span class="pln"> <input type="text" name="Cat_meta[extra2]" id="Cat_meta[extra2]" size="25" style="width:60%;" value="</span><span class="pun"><?</span><span class="pln">php echo $cat_meta</span><span class="pun">[</span><span class="str">'extra2'</span><span class="pun">]</span> <span class="pun">?</span><span class="pln"> $cat_meta</span><span class="pun">[</span><span class="str">'extra2'</span><span class="pun">]</span> <span class="pun">:</span> <span class="str">''</span><span class="pun">;</span> <span class="pun">?></span><span class="pln">"></span><span class="tag"><br</span> <span class="tag">/></span> <span class="tag"><span</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"description"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'extra field'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></span></span> <span class="tag"></td></span> <span class="tag"></tr></span> <span class="tag"><tr</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"form-field"</span><span class="tag">></span> <span class="tag"><th</span> <span class="atn">scope</span><span class="pun">=</span><span class="atv">"row"</span> <span class="atn">valign</span><span class="pun">=</span><span class="atv">"top"</span><span class="tag">><label</span> <span class="atn">for</span><span class="pun">=</span><span class="atv">"extra3"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'extra field'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></label></th></span> <span class="tag"><td></span> <span class="tag"><textarea</span> <span class="atn">name</span><span class="pun">=</span><span class="atv">"Cat_meta[extra3]"</span> <span class="atn">id</span><span class="pun">=</span><span class="atv">"Cat_meta[extra3]"</span> <span class="atn">style</span><span class="pun">=</span><span class="atv">"</span><span class="kwd">width</span><span class="pun">:</span><span class="lit">60%</span><span class="pun">;</span><span class="atv">"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php echo $cat_meta</span><span class="pun">[</span><span class="str">'extra3'</span><span class="pun">]</span> <span class="pun">?</span><span class="pln"> $cat_meta</span><span class="pun">[</span><span class="str">'extra3'</span><span class="pun">]</span> <span class="pun">:</span> <span class="str">''</span><span class="pun">;</span> <span class="pun">?></span><span class="tag"></textarea><br</span> <span class="tag">/></span> <span class="tag"><span</span> <span class="atn">class</span><span class="pun">=</span><span class="atv">"description"</span><span class="tag">></span><span class="pun"><?</span><span class="pln">php _e</span><span class="pun">(</span><span class="str">'extra field'</span><span class="pun">);</span> <span class="pun">?></span><span class="tag"></span></span> <span class="tag"></td></span> <span class="tag"></tr></span> <span class="pun"><?</span><span class="pln">php </span><span class="pun">}</span> |
As you can see i added 4 new fields and all of them are in an array Cat_meta[key] because that way we only create on row in the options table to save all of the category’s extra fields instead of a row for each field.
Next we need to save the extra fields in to the database once a user submits the category edit form and we do that using “edited_category” with a function that will run through each of the submitted fields and insert them to the database using the update_option function, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span class="pun"><?</span><span class="pln">php </span><span class="com">// save extra category extra fields hook</span><span class="pln"> add_action </span><span class="pun">(</span> <span class="str">'edited_category'</span><span class="pun">,</span> <span class="str">'save_extra_category_fileds'</span><span class="pun">);</span> <span class="com">// save extra category extra fields callback function</span> <span class="kwd">function</span><span class="pln"> save_extra_category_fileds</span><span class="pun">(</span><span class="pln"> $term_id </span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln"> isset</span><span class="pun">(</span><span class="pln"> $_POST</span><span class="pun">[</span><span class="str">'Cat_meta'</span><span class="pun">]</span> <span class="pun">)</span> <span class="pun">)</span> <span class="pun">{</span><span class="pln"> $t_id </span><span class="pun">=</span><span class="pln"> $term_id</span><span class="pun">;</span><span class="pln"> $cat_meta </span><span class="pun">=</span><span class="pln"> get_option</span><span class="pun">(</span> <span class="str">"category_$t_id"</span><span class="pun">);</span><span class="pln"> $cat_keys </span><span class="pun">=</span><span class="pln"> array_keys</span><span class="pun">(</span><span class="pln">$_POST</span><span class="pun">[</span><span class="str">'Cat_meta'</span><span class="pun">]);</span> <span class="kwd">foreach</span> <span class="pun">(</span><span class="pln">$cat_keys </span><span class="kwd">as</span><span class="pln"> $key</span><span class="pun">){</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">isset</span><span class="pun">(</span><span class="pln">$_POST</span><span class="pun">[</span><span class="str">'Cat_meta'</span><span class="pun">][</span><span class="pln">$key</span><span class="pun">])){</span><span class="pln"> $cat_meta</span><span class="pun">[</span><span class="pln">$key</span><span class="pun">]</span> <span class="pun">=</span><span class="pln"> $_POST</span><span class="pun">[</span><span class="str">'Cat_meta'</span><span class="pun">][</span><span class="pln">$key</span><span class="pun">];</span> <span class="pun">}</span> <span class="pun">}</span> <span class="com">//save the option array</span><span class="pln"> update_option</span><span class="pun">(</span> <span class="str">"category_$t_id"</span><span class="pun">,</span><span class="pln"> $cat_meta </span><span class="pun">);</span> <span class="pun">}</span> <span class="pun">}</span> |
From the code above you can see that all of the extra fields we’ve added are stored in the database’s options table with the name ‘category_ID’ , where ID is the id of the specific category we just edited and that means we can call this data in our plugins or theme files easily using the get_option function.
say for example my category id is 25 then my code will look like
1 |
<span class="pun"><?</span><span class="pln">php $cat_data </span><span class="pun">=</span><span class="pln"> get_option</span><span class="pun">(</span><span class="str">'category_25'</span><span class="pun">);</span> <span class="pun">?></span> |
As I stated in the beginning, I need to display a different image for each category, so in that case I added these few lines of code to my theme’s category.php right after the code that displays the category title:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="pun"><?</span><span class="pln">php </span><span class="com">//first get the current category ID</span><span class="pln"> $cat_id </span><span class="pun">=</span><span class="pln"> get_query_var</span><span class="pun">(</span><span class="str">'cat'</span><span class="pun">);</span> <span class="com">//then i get the data from the database</span><span class="pln"> $cat_data </span><span class="pun">=</span><span class="pln"> get_option</span><span class="pun">(</span><span class="str">"category_$cat_id"</span><span class="pun">);</span> <span class="com">//and then i just display my category image if it exists</span> <span class="kwd">if</span> <span class="pun">(</span><span class="pln">isset</span><span class="pun">(</span><span class="pln">$cat_data</span><span class="pun">[</span><span class="str">'img'</span><span class="pun">])){</span><span class="pln"> echo </span><span class="str">'<div class="category_image"><img src="'</span><span class="pun">.</span><span class="pln">$cat_data</span><span class="pun">[</span><span class="str">'img'</span><span class="pun">].</span><span class="str">'"></div>'</span><span class="pun">;</span> <span class="pun">}</span> |
Nice and easy and we are all done. The result should look similar to this:
Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ. Các bạn đi ngang nếu được cho Lâm 1 like và 1 đăng ký kênh Youtube nhé !!