2014-02-21 12 views
0

你好我試圖在wordpress中創建一個自定義字段面板。但是當我在函數中添加代碼時,任何人都可以幫我一把嗎?如何:在wordpress中自定義字段面板

這是我試圖

的代碼,但沒有在管理面板出現

$key = "videojuegos"; 
$meta_boxes = array(
"plataforma" => array(
    "nombre" => "plataforma", 
    "titulo" => "Plataforma", 
    "descripcion" => "Plataforma del videojuego - XBox360, Playstation3, PC, etc..."), 
"precio" => array(
    "nombre" => "precio", 
    "titulo" => "Precio", 
    "descripcion" => "Precio del videojuego en Euros."), 
"edad" => array(
    "nombre" => "edad", 
    "titulo" => "Edad Recomendada", 
    "descripcion" => "Edad recomendada del videojuego"), 
"idioma" => array(
    "nombre" => "idioma", 
    "titulo" => "Idioma", 
    "descripcion" => "Idioma del videojuego") 
); 

function crear_meta_box() { 
    global $key; 

    if(function_exists('add_meta_box')) { 
     add_meta_box('nuevo-meta-boxes', ucfirst($key) . ' Características', 'mostrar_meta_box', 'videojuegos', 'normal', 'high'); 
    } 
} 
function mostrar_meta_box() { 
global $post, $meta_boxes, $key; 
?> 

<div class="form-wrap"> 

<?php 
wp_nonce_field(plugin_basename(__FILE__), $key . '_wpnonce', false, true); 

foreach($meta_boxes as $meta_box) { 
    $data = get_post_meta($post->ID, $key, true); 
    ?> 

    <div class="form-field form-required"> 
     <label for="<?php echo $meta_box[ 'nombre' ]; ?>"><?php echo $meta_box[ 'titulo' ]; ?></label> 
     <input type="text" name="<?php echo $meta_box[ 'nombre' ]; ?>" value="<?php echo htmlspecialchars($data[ $meta_box[ 'nombre' ] ]); ?>" /> 
     <p><?php echo $meta_box[ 'descripcion' ]; ?></p> 
    </div> 

<?php } // Fin del foreach?> 
</div> 
<?php 
} // Fin de la función mostrar_meta_box 

function dirigido_custom_box_mostrar($post) { 
    $valor_dirigido = get_post_meta($post->ID, 'valor_dirigido', true); 
     wp_nonce_field('save_dirigido_meta', 'dirigido_nonce'); 
    ?> 
     <?php wp_editor($valor_dirigido, 'valor_dirigido', array('media_buttons' => false, 'textarea_name' => 'valor_dirigido')); ?> 
    <?php 
} 


function grabar_meta_box($post_id) { 
    global $post, $meta_boxes, $key; 

    foreach($meta_boxes as $meta_box) { 
     $data[ $meta_box[ 'nombre' ] ] = $_POST[ $meta_box[ 'nombre' ] ]; 
    } 

    if (!wp_verify_nonce($_POST[ $key . '_wpnonce' ], plugin_basename(__FILE__))) 
     return $post_id; 

    if (!current_user_can('edit_post', $post_id)) 
     return $post_id; 

    update_post_meta($post_id, $key, $data); 
} 
add_action('admin_menu', 'crear_meta_box'); 
add_action('save_post', 'grabar_meta_box'); 

回答

1
if (!function_exists("custom_dashboard_widget")) { 
function custom_dashboard_widget() { 
    $current_user = wp_get_current_user(); 
    echo 'Username: ' . $current_user->user_login . '<br />'; 
    echo 'User email: ' . $current_user->user_email . '<br />'; 
    echo 'User first name: ' . $current_user->user_firstname . '<br />'; 
    echo 'User last name: ' . $current_user->user_lastname . '<br />'; 
    echo 'User display name: ' . $current_user->display_name . '<br />'; 
    echo 'User ID: ' . $current_user->ID . '<br />'; 
    } 
} 



if(!function_exists("add_custom_dashboard_widget")) { 
    function add_custom_dashboard_widget() { 
     wp_add_dashboard_widget('custom_dashboard_widget',"Client's DashBoard", 'custom_dashboard_widget'); 
     } 
    } 
//This actually adds your dashboard panel 
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget'); 

簡單的例子WordPress的儀表板自定義窗口小部件。
只需複製並在您的主題根目錄中的function.php文件中添加以上代碼即可。
這裏我附上了一個圖像,顯示您的小部件在儀表板區域中的顯示方式。 enter image description here

+0

總是使用條件檢查與function_exists()* BEFORE *創建一個函數,以避免函數名稱衝突。 – bodi0

+0

非常好... 感謝@bodi爲您的建議。 –

相關問題