方法:在wordpress中自定义字段面板

时间:2014-02-21 09:17:15

标签: php 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 个答案:

答案 0 :(得分: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