【问题标题】:WordPress add_action style arguments works but changes admin panelWordPress add_action 样式参数有效,但更改了管理面板
【发布时间】:2018-04-09 20:00:51
【问题描述】:

我已经编写了一个代码,以便您可以轻松地向网站添加样式表,这是用于我们的框架的。

它有效,但如果我使用它,CSS 是否也会更改仪表板布局?与定制器相同,如果我不使用此方法,但只是一个普通的 wp enqueue 它可以工作,但这不是我想要的。

    class theme_setup{
        function main_css_setup($css) {
            wp_enqueue_style( 'style', get_template_directory_uri().'/css/'.$css.'.css' );
        }
        function add_css_main($css){
            add_action( 'wp_enqueue_scripts', array($this, 'main_css_setup'),10,1 );
            do_action( 'wp_enqueue_scripts', $css );
        }
    }
$theme_setup = new theme_setup();
$theme_setup->add_css_main('main');

仪表板更改: First example Second example

我认为这与do_action 有关,我搜索了我的最佳行为,但没有任何运气。

我正在使用“do_action”传递用户使用add_css_main 提供的参数,我知道我可以使用全局变量,但这些是“静态的”,这将使这个想法消失灵活的功能。

更新:

function themename_custom_logo_setup($height,$width) {
    $defaults = array(
        'height'      => $height,
        'width'       => $width,
        'flex-height' => true,
        'flex-width'  => true,
        'header-text' => array( 'site-title', 'site-description' ),
    );
    add_theme_support( 'custom-logo', $defaults );
}

    function add_custom_logo($width,$height){
    add_action( 'after_setup_theme', array($this, 'themename_custom_logo_setup' ),10,2);
    do_action( 'after_setup_theme', $height,$width);
}

$theme_setup->add_custom_logo(88,250);

确实有效,但变量根本没有传递。

【问题讨论】:

  • 你为什么使用do_action
  • @SamvelAleqsanyan 要传递参数,找不到任何其他可行的方法,期望..创建一个全局变量,但函数的意义就消失了。
  • 使用你的类属性传递参数
  • 正如我所说,我不想使用变量或全局变量,因为这会带走“灵活”部分。
  • @SamvelAleqsanyan 看到你的答案,是的,它可以工作 2,但是.. 如果我使用 add_css_main 不止一次,只有最后一个工作..

标签: php wordpress class wordpress-theming


【解决方案1】:

这应该避免使用this conditional 加载CSS (可能在代码中的其他地方应用它)

function add_css_main($css){
    if( !is_admin() ) {
        add_action( 'wp_enqueue_scripts', array($this, 'main_css_setup'),10,1 );
        do_action( 'wp_enqueue_scripts', $css );
    }
}

【讨论】:

  • ps:您使用的 do_action 可能没有按照您的想法进行操作...您读过这个吗? Actions and filters are NOT the same thing…
  • 如果您不想在后端加载 CSS 文件,is_admin 是解决问题的方法。如果你需要在后端加载 CSS,你必须确保你的类名和元素不与 WordPress 自己的 CSS 冲突。
  • 已经感谢您的帮助,您是英雄,但是.. 我添加了一个新的更新,您或 samvel 可以检查一下吗?
猜你喜欢
  • 2018-05-18
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 2013-10-13
  • 2018-11-09
  • 1970-01-01
相关资源
最近更新 更多