网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

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

GG网络技术分享 2025-03-18 16:12 2


问题描述:

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

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我创建了我的自定义插件并希望添加自定义徽标,但不知何故它会将我重定向到错误的目录,这是 错误我得到:</ p>

获取 http:// localhost / logo .png 404(未找到)</ p>

</ blockquote>

这是我到目前为止所尝试的内容:</ p>

   $ 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__);

</ code> </ pre>

这是我的添加操作:</ p>

  function kalbos_modifymenu(){

add_menu_page(\'Kalbos\',// page title

\'Kalbos\',// menu title

\'manage_options\',// capabilities

\'kalbos_list\', // menu slug

\'kalbos_list\',// function

$ plugin_dir.\'login.png\',

\'5\'

);

}

add_action(\'admin_menu\',\'kalbos_modifymenu\'); \\ n </ code> </ pre>

然而,当我将我的徽标移动到wp-admin / logo.png dir时,图标正常工作,因为即使我将其设置为我的插件,我的徽标路径也会转到我的插件 文件夹</ p>

</ div>

网友观点:

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/

###

Try to add an other slash to /plugins like this: /plugins/ and also close your quotes on the same line. At the moment you have it like \'plugins; but the correct is \'/plugins\';

标签:

提交需求或反馈

Demand feedback