0
我已經創建了一個自定義頁面使用add_theme_page功能,我目前堅持談到將信息添加到數據庫。我發現這篇文章上WordPress Codex保存自定義管理頁面信息到數據庫
如果你看到
<?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?>
第一個參數需要$ POST_ID的聯繫,但對我來說,我沒有後ID。
下面是當前的代碼。
add_action('admin_menu', 'my_plugin_menu');
function my_plugin_menu() {
add_theme_page('Social Media', 'Social Media', 'edit_theme_options', 'social-media-links', 'social_media_links');
}
function social_media_links(){
if(isset($_POST["wipit"]) and $_POST["wipit"] == "theme-options"){
$facebook = $_POST["facebook"];
$twitter = $_POST["twitter"];
$youtube = $_POST["youtube"];
$pinterest = $_POST["pinterest"];
$facebook = sanitize_text_field($facebook);
$twitter = sanitize_text_field($twitter);
$youtube = sanitize_text_field($youtube);
$pinterest = sanitize_text_field($pinterest);
$facebook = esc_url($facebook);
$twitter = esc_url($twitter);
$youtube = esc_url($youtube);
$pinterest = esc_url($pinterest);
}
?>
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/resources/css/bootstrap.css">
<link rel="stylesheet" href="<?php echo get_template_directory_uri(); ?>/resources/css/admin.css">
<script src="<?php echo get_template_directory_uri(); ?>/resources/js/vendor/bootstrap.min.js"></script>
<div class="wrap" id="theme-options-wrap">
<h2>Social Media</h2>
<ul class="nav nav-tabs">
<li class="active"><a href="#social" data-toggle="tab">Social Media</a></li>
<li><a href="#profile" data-toggle="tab">Profile</a></li>
<li><a href="#messages" data-toggle="tab">Messages</a></li>
<li><a href="#settings" data-toggle="tab">Settings</a></li>
</ul>
<form method="post" enctype="multipart/form-data" action="<?php $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" value="theme-options" name="wipit"/>
<div class="tab-content" id="social-media">
<div class="tab-pane fade in active" id="social">
<div class="row">
<div class="col-md-4"><label>Facebook</label></div>
<div class="col-md-8"><input type="email" class="form-control" id="facebook" name="facebook"></div>
</div>
<div class="row">
<div class="col-md-4"><label>Twitter</label></div>
<div class="col-md-8"><input type="email" class="form-control" id="twitter" name="twitter"></div>
</div>
<div class="row">
<div class="col-md-4"><label>YouTube</label></div>
<div class="col-md-8"><input type="email" class="form-control" id="youtube" name="youtube"></div>
</div>
<div class="row">
<div class="col-md-4"><label>Pinterest</label></div>
<div class="col-md-8"><input type="email" class="form-control" id="pinterest" name="pinterest"></div>
</div>
</div>
<div class="tab-pane fade" id="profile">...</div>
<div class="tab-pane fade" id="messages">...</div>
<div class="tab-pane fade" id="settings">...</div>
</div>
<div class="theme-option-button"><button type="submit" class="btn btn-primary btn-lg">Save Changes</button></div>
</form>
</div>
<?php
}
?>