为这个简单的插件创建一个选项页面

时间:2019-01-26 15:44:37

标签: php wordpress

我想创建一个选项页面来编辑$date变量,并更改此简单插件的图像src,请帮助php noob =)

<?php
  /*
  Plugin Name: Masthead for xxx
  Description: Masthead for xxx
  Author: Red Mariachi
  Version: 0.2 [26/01/2019]
  */
  add_action( 'avada_before_header_wrapper', 'avada_add_banner' );
  function avada_add_banner() {

  $date='24/01/19';

  if(\DateTime::createFromFormat('d/m/y',$date) > new \DateTime()){
    //data passata
    echo '<img class="size-full wp-image-40510 aligncenter" src="http://xxx.it/xxx/wp-content/uploads/2019/01/backup-1.jpg" alt="" width="100%" height="356" />';
  } else{
    //data è odierna o futura
    echo '<h1 style="text-align:center; font-size:40px;">BANNER SCADUTO</h1>';
  }
}

1 个答案:

答案 0 :(得分:0)

/**
 * Register a custom menu page.
 */
function my_custom_menu_page(){
    add_menu_page( 
        __( 'Custom Menu Title', 'textdomain' ),
        'custom menu',
        'manage_options',
        'custompage',
        'my_custom_menu_page_html',
        plugins_url( 'myplugin/images/icon.png' ),
        6
    ); 
}
add_action( 'admin_menu', 'my_custom_menu_page' );

/**
 * Display a custom menu page
 */
function my_custom_menu_page_html(){
    ?>
    <form method="POST" action="admin-post.php">
    <input type="hidden" name="action" value="update_my_date_action">
    <input type="date" name="my_date">
    <input type="submit" value="Submit">
    <?php
}

function update_my_date_action(){
update_option( 'my_custom_date', $_POST['my_date']);
}

add_action( 'admin_post_update_my_date_action', 'update_my_date_action' );

[根据需要处理您的POST数据]

您必须查看文档以了解更多详细信息。

https://developer.wordpress.org/reference/functions/add_menu_page/

https://codex.wordpress.org/Function_Reference/update_option

https://developer.wordpress.org/reference/functions/get_option/