douchan7552 2013-01-25 01:37
浏览 25
已采纳

自定义Wordpress插件,他们都只是小资? [关闭]

I am trying my hand at Wordpress plugin development. From all of the getting started examples and tutorials I have seen, it seems as though plugins can do little more than, say, add a "Bookmark This" at the end of a post, add an icon next to external links, or other relatively minor enhancements to existing features.

I would like to develop an extension (might that be the better word?) where a user can go to my-wordpress-site.com/the-extension and access the features of the web application. However, since all extensions must placed within wp-includes/plugins/the-extension, does that mean that is the only way the plugins can be accessed?

How could I design the extension and containing pages to be accessed with a URL like this: my-wordpress-site.com/the-extension?

  • 写回答

3条回答 默认 最新

  • dongshi3818 2013-01-25 02:11
    关注

    Here's an example:

    // register your rewrite rule
    add_action('init', function(){
      add_rewrite_rule('the-extension$', 'index.php?my_plugin_action=do_stuff', 'top');
    });
    
    // register your query variable
    add_filter('query_vars', function($vars){
      $vars[] = 'my_plugin_action';
      return $vars;
    });
    
    // process request
    add_action('parse_request', function($wp){
    
      // check if this is your request; do nothing if not
      if(!isset($wp->query_vars['my_plugin_action']))
        return;
    
      // otherwise do your stuff
      printf('Hello Pony. You requested "%s"', $wp->query_vars['my_plugin_action']);
      exit;
    });
    

    This will only work after you go to your dashboard > settings > permalinks and click the save button to flush rewrite rules, because WP apparently keeps some kind of cache of all rewrite rules.

    Then accessing yourwordpresssite.com/the-extension should display the hello message

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大