WordPress賦予用戶直接在管理界面內的帖子,頁面和自定義帖子類型創建自定義元框。 WordPress API還為默認的元框提供了廣泛的自定義選項。該教程指導您通過創建,保存,驗證和檢索自定義元數據,並刪除默認的元框。
>密鑰概念:
- 自定義元框,通過允許在帖子,頁面和自定義帖子類型中添加額外信息來擴展WordPress功能。 這些顯示在WordPress儀表板中。
- 涉及編碼時,諸如高級自定義字段,元框和CMB2之類的插件大大簡化了該過程。 主題文件修改對於在前端顯示自定義元框數據是必要的。 > 自定義元盒功能強大,但有局限性。 他們無法更改WordPress管理接口或添加不支持的功能。 數據驗證和消毒對安全至關重要。
- >
了解自定義元框:>
自定義元框提供了一種添加WordPress默認選項以外的字段的方法。插件和主題利用它們收集結構化的用戶輸入。 它們也可以添加到儀表板中,以反映WordPress儀表板小部件的功能(本質上是元框本身)。 標準的元框包括編輯器,自定義字段,特色圖像,類別和標籤部分。
>自定義元框與自定義字段:
自定義字段存儲鍵值數據對。 Meta框提供更多的通用輸入類型,包括顏色拾取器,文件上傳和下拉列表。
元數據解釋了:
>元數據表示輸入的值中的值。 WordPress將此數據存儲為鍵值對(元密鑰和元值),其中元密鑰是字段名稱,元值是輸入的數據。
創建一個元框:>
函數寄存器並顯示自定義元框。 以下代碼將自定義的元框添加到WordPress帖子:add_meta_box
>
function custom_meta_box_markup() { // Content will be added here later } function add_custom_meta_box() { add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "post", "side", "high", null); } add_action("add_meta_boxes", "add_custom_meta_box");>
使用七個參數:id(唯一標識符),標題(顯示標題),回調(函數顯示內容),屏幕(帖子類型),context(position),優先級(上下文中的順序)和回調參數。 add_meta_box
>將字段添加到自定義元框:
此代碼包括對安全性的NONCE驗證,並使用
來檢索現有數據。function custom_meta_box_markup($object) { wp_nonce_field(basename(__FILE__), "meta-box-nonce"); ?> <div> <label for="meta-box-text">Text:</label> <input name="meta-box-text" type="text" value="<?php echo esc_attr(get_post_meta($object->ID, "meta-box-text", true)); ?>"> <br><br> <label for="meta-box-dropdown">Dropdown:</label> <select name="meta-box-dropdown"> <?php $option_values = array(1, 2, 3); foreach ($option_values as $value) { $selected = ($value == get_post_meta($object->ID, "meta-box-dropdown", true)) ? 'selected' : ''; echo "<option value=\"{$value}\" {$selected}>{$value}</option>"; } ?> </select> <br><br> <label for="meta-box-checkbox">Check Box:</label> <?php $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true); $checked = ($checkbox_value == "true") ? 'checked' : ''; ?> <input name="meta-box-checkbox" type="checkbox" value="true" <?php echo $checked; ?>> </div> <?php }
get_post_meta
>保存元數據:
掛鉤保存數據:save_post
function custom_meta_box_markup() { // Content will be added here later } function add_custom_meta_box() { add_meta_box("demo-meta-box", "Custom Meta Box", "custom_meta_box_markup", "post", "side", "high", null); } add_action("add_meta_boxes", "add_custom_meta_box");此代碼可驗證NONCE,用戶權限和自動信號。 至關重要的是,它使用
來防止安全漏洞。 sanitize_text_field
>
>刪除元盒:
函數刪除了元框。 例如,要刪除自定義字段元框:remove_meta_box
function custom_meta_box_markup($object) { wp_nonce_field(basename(__FILE__), "meta-box-nonce"); ?> <div> <label for="meta-box-text">Text:</label> <input name="meta-box-text" type="text" value="<?php echo esc_attr(get_post_meta($object->ID, "meta-box-text", true)); ?>"> <br><br> <label for="meta-box-dropdown">Dropdown:</label> <select name="meta-box-dropdown"> <?php $option_values = array(1, 2, 3); foreach ($option_values as $value) { $selected = ($value == get_post_meta($object->ID, "meta-box-dropdown", true)) ? 'selected' : ''; echo "<option value=\"{$value}\" {$selected}>{$value}</option>"; } ?> </select> <br><br> <label for="meta-box-checkbox">Check Box:</label> <?php $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true); $checked = ($checkbox_value == "true") ? 'checked' : ''; ?> <input name="meta-box-checkbox" type="checkbox" value="true" <?php echo $checked; ?>> </div> <?php }請記住,使用的鉤子(例如,
或do_meta_boxes
)取決於上下文。
wp_dashboard_setup
WordPress的Meta Box API是擴展功能的強大工具。 了解其細微差別並確定安全性最佳實踐是有效實施的關鍵。 提供的代碼示例說明了核心過程,但請記住將它們適應您的特定需求並始終對用戶輸入進行消毒。
以上是將自定義的元框添加到WordPress Admin界面的詳細內容。更多資訊請關注PHP中文網其他相關文章!

WordPressiser-FrightlyDuetLoitsIntuitiveInterfaceAndcms,whosparateContentFromDesign.itoffersArichTextedextEditorforeasyContentCreationAndialibraryFororRaryFororRory.itsflexeNhangedBancedBynhangedBynHangedBynumereNumerSandeMesandPlugins,elloverSandplugins,elloverOverUseCanimpActpercrance

1)ITSUPPORTSE-CommerceWithPluginSlikeWooCommerce,允許ProductManagementAndAndPaymentProcessing.2)ITSERVESASASASACMSACMSACMSMSACMSMSACMSMASTRATATEBLOGS,增強SeoAndEngagement.3)

WordPressIsnotIdeAlforHigh-TrafficWebsites,customAndCompleXapplications,安全性 - 敏感性plicestions,Real-Timedataprocessing,AndhighlyCustomizeduserInterfaces.forhigh-Trafficsites,USENEENEXT.JSORXOLX.JSORCOSTOMSOLTICTS; forCompleXapplications; forcomplexapplications; forcomplexapplications,optfordjangoorrub

Yes,youcanbuildablogwithWordPress.1)ChoosebetweenWordPress.comforbeginnersorWordPress.orgformorecontrol.2)Selectathemetopersonalizeyourblog'slook.3)Usepluginstoenhancefunctionality,likeSEOandsocialmediaintegration.4)Customizeyourthemewithsimplecodetw

WordPressCanbeseCureifManagedProperly.1)keepthewordPressCoreUpdatedTopatchVulnerabilities.2)vetandupdatepluginsandthemesfromreputables.3)EnforcestrongpasseTSandusetWordssandusetWordwordwo-factorauthenticaliation.4)

WordPressCanbuildVariousTypesofwebsites:1)個人博客,EasyTosetUpWithTheMesandPlugins.2)BusinessWebsites,使用drag-and-dropbuilders.3)e-commercePlatforms,forwoocommerceforsemcommerceforseameamseamelesssites.4)communitySites.4)conduction.4)使用bbudicatipration

WordPressisapowerfulCMSwithsignificantadvantagesandchallenges.1)It'suser-friendlyandcustomizable,idealforbeginners.2)Itsflexibilitycanleadtositebloatandsecurityissuesifnotmanagedproperly.3)Regularupdatesandperformanceoptimizationsarenecessarytomainta

WordPressExcccelineaseeandAdaptability,MakeitItiDealForBeginnersandsMallToMedium-SizedBusinesses.1)siseofuse:wordpressisuser-Frylyly.2)安全:drupalleadswithstrongsecurityfeatures.3)性能:performance:performance formation:phast offersefersefersefersefersefersefersefersefersexcellentperformanceedueTonode.sscore.jssor.jjsy.jjsy.4)4)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具