dongxiaowei_1234 2018-04-17 09:00 采纳率: 0%
浏览 84
已采纳

在自定义WordPress插件中加载图像

I created my custom plugin and want to add custom logo, but somehow it keeps redirecting me to wrong dir, this is error I get:

GET http://localhost/logo.png 404 (Not Found)

Here is what I tried so far:

$plugin_dir = str_replace( $base_url, ABSPATH, $plugins_url );
$plugin_dir = plugins_url();
$plugin_dir = plugin_dir_path( __FILE__ );
$plugin_dir = WP_CONTENT_DIR . '/plugins';
$plugin_dir = plugins_url( '/', __FILE__ );

Here is my add action:

    function kalbos_modifymenu() {

        add_menu_page('Kalbos', //page title
            'Kalbos', //menu title
            'manage_options', //capabilities
            'kalbos_list', //menu slug
            'kalbos_list', //function
            $plugin_dir . 'logo.png',
            '5'
            );
}
    add_action('admin_menu','kalbos_modifymenu');

However icon is working when i move my logo to wp-admin/logo.png dir because thats where my logo path goes even if i set it go to my plugin folder

  • 写回答

2条回答 默认 最新

  • dongyou2279 2018-04-17 14:15
    关注

    plugins_url will (my emphasis):

    Retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.

    So, the following will point exactly to your plugin directory with an ending slash:

    $plugin_dir = plugins_url( '/', __FILE__ );
    // results in http://localhost/wp-content/plugins/YOUR_PLUGIN/
    

    And to target the logo, use:

    $plugin_dir . 'logo.png'
    

    Another example, to make a shortcut to your images folder:

    $plugin_img = plugins_url( '/images/', __FILE__ );
    // results in http://localhost/wp-content/plugins/YOUR_PLUGIN/images/
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题