wordpress 为文章添加seo

 

用到了3个方法
add_meta_box( string $id, string $title, callable $callback, string|array|WP_Screen $screen = null, string $context = 'advanced', string $priority = 'default', array $callback_args = null )

get_post_meta( int $post_id, string $key = '', bool $single = false )
update_post_meta( $post_id, $meta_key, $meta_value, $prev_value );

new sonliss_seo();

class sonliss_seo
{
  public function __construct()
  {
    add_action( 'add_meta_boxes', [$this, 'sonliss_meda_box'] );
    add_action( 'save_post', [$this, 'sonliss_save_meda'] );
  }
  /**
   * 添加表单
   */
  public function sonliss_meda_box($post_type)
  {
    #var_dump($post_type);
    $post_types = ['post', 'page'];
    if(in_array($post_type, $post_types))
    {
      add_meta_box( 
        'seo_box',  // ID
        'seo设置',  // 块的标题
        [ $this, 'sonliss_seo_list' ], // 回调
        $post_type, // 显示页面, 默认当前页面
        'side', // 显示位置 Post edit screen contexts include 'normal', 'side', and 'advanced'
        'high',  // 优先级, 'high', 'low', 默认default
        null // 回调参数, 默认 null
      );
    }
  }
  /**
   * 保存表单
   */
  public function sonliss_save_meda($post_id)
  {
    // 检查权限
    if ( 'page' == $_POST['post_type'] ) 
    {
      if ( ! current_user_can( 'edit_pages', $post_id ) ) 
      {
        return $post_id;
      }
    } else 
    {
      if ( ! current_user_can( 'edit_posts', $post_id ) ) 
      {
        return $post_id;
      }
    }

    $data = [
      'seo_title' => $_POST['seo_title'],
      'seo_keywords' => $_POST['seo_keywords'],
      'seo_description' => $_POST['seo_description']
    ];
    
    foreach($data as $key => $val)
    {
      update_post_meta( 
        $post_id, // post id
        $key, // 表单name
        $val, // 表单value
        '' // 表单旧value
      );
    }
  }
  /**
   * 表单列表
   */
  public function sonliss_seo_list($post)
  {
    $seo_list = [
      'seo_title' => 'seo标题',
      'seo_keywords' => 'seo关键字',
    ];
    $seo_list_textarea = ['seo_description' => 'seo描述'];
    foreach($seo_list as $key => $val)
    {
      $this->sonliss_get_post_meda($post->ID, $key, $val);
    }
    foreach($seo_list_textarea as $key => $val)
    {
      $this->sonliss_get_post_meda($post->ID, $key, $val, 'textarea');
    }
  }
  /**
   * 获取自定义表单
   */
  public function sonliss_get_post_meda($post_id, $key, $label, $type = 'text')
  {
    $value = get_post_meta( 
      $post_id,  // post id
      $key, // 表单name
      true  // 是否单一值
    );
    if($type == 'textarea')
    {
      echo $this->sonliss_textareabox_html($key, $value, $label);
    } else 
    {
      echo $this->sonliss_textbox_html($key, $value, $label);
    }
  }
  /**
	 * input html
	 */
	public function sonliss_textbox_html($name, $value, $label) {
    $html = '<div class="components-form-token-field">';
    $html .= '<label class="components-form-token-field__label">'. $label .'</label>';
    $html .= '<div class="components-form-token-field__input-container">';
    $html .= '<input type="text" id="'. $name .'" name="'. $name .'" value="'. $value .'" class="components-form-token-field__input" />';
    $html .= '</div>';
    $html .= '</div>';
    return $html;
  }

	/**
	 * textarea html
	 */
	public function sonliss_textareabox_html($name, $value, $label) {
		$html = '<div class="components-base-control editor-post-excerpt__textarea">';
    $html .= '<div class="components-base-control__field">';
    $html .= '<label class="components-base-control__label">'. $label .'</label>';
    $html .= '<textarea id="'. $name .'" name="'. $name .'" class="components-textarea-control__input" >'. $value .'</textarea>';
    $html .= '</div>';
    $html .= '</div>';
    return $html;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值