如何在不需要wp-Load的情况下获得选项值?

时间:2017-07-13 作者:Prisma

我正在开发主题,以便在之前进行销售。我想在头部获取选项值。但由于checkoptions尚未调用,我需要调用wp load。php。但来自themeforest的审查人员表示,wp负载。在任何情况下都不应调用php。因此,您可以指导我如何在不需要wp负载的情况下执行相同的操作。php?

这是我的女朋友

<?php 

$root = dirname(dirname(dirname(dirname(dirname(__FILE__)))));

if ( file_exists( $root.\'/wp-load.php\' ) ) {
    require_once( $root.\'/wp-load.php\' );
} else {
    $root = dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))));
    if ( file_exists( $root.\'/wp-load.php\' ) ) {
    require_once( $root.\'/wp-load.php\' );
    }
}

$checkOpt = get_option(\'Bethaven\');

    header( "Content-type: text/css; charset: UTF-8" );
    global $post;
//Footer font color
if (get_post_meta($post->ID, \'oebryn_footer_font_color\', true) != \'\' ) {
    $footer_font_color = get_post_meta($post->ID, \'oebryn_footer_font_color\', true);
}
elseif ( $checkOpt[\'oebryn_footer_font_color\'] != \'\' ) {
    $footer_font_color = $checkOpt[\'oebryn_footer_font_color\'];
}
// Footer link color
if (get_post_meta($post->ID, \'oebryn_footer_link_color\', true) != \'\' ) {
    $footer_font_color = get_post_meta($post->ID, \'oebryn_footer_link_color\', true);
}
elseif ( $checkOpt[\'oebryn_footer_link_color\'] != \'\' ) {
    $footer_font_color = $checkOpt[\'oebryn_footer_link_color\'];
}
// Footer hovered color
if (get_post_meta($post->ID, \'oebryn_footer_link_hover\', true) != \'\' ) {
    $footer_hovered = get_post_meta($post->ID, \'oebryn_footer_link_hover\', true);
}
elseif ( $checkOpt[\'oebryn_footer_link_hover\'] != \'\' ) {
    $footer_hovered = $checkOpt[\'oebryn_footer_link_hover\'];
}
// Widget title color
if (get_post_meta($post->ID, \'oebryn_footer_widget_title\', true) != \'\' ) {
    $footer_title = get_post_meta($post->ID, \'oebryn_footer_widget_title\', true);
}
elseif ( $checkOpt[\'oebryn_footer_widget_title\'] != \'\' ) {
    $footer_title = $checkOpt[\'oebryn_footer_widget_title\'];
}

?>

#header-bar nav ul#oebryn-navigation > li >a {
    color: <?php echo esc_attr($checkOpt[\'oebryn_header_color\'] );?> !important;
}

#oebryn-navigation ul.sub-menu {
    background-color: <?php echo esc_attr($checkOpt[\'oebryn_submenu_background\']); ?> !important;
}
#header-bar ul#oebryn-navigation li ul.sub-menu li ul.sub-menu a, #header-bar ul#oebryn-navigation li ul.sub-menu li a {
    color: <?php echo esc_attr($checkOpt[\'oebryn_submenu_color\'] );?> !important;
}
ul#oebryn-navigation li ul.sub-menu li ul.sub-menu li:hover, #header-bar ul#oebryn-navigation li ul.sub-menu li:hover {
    border-bottom: 1px <?php echo esc_attr($checkOpt[\'oebryn_submenu_hover_color\'] );?> solid !important;
}
#header-bar ul#oebryn-navigation li.megamenu ul.sub-menu li >a {
    color: <?php echo esc_attr($checkOpt[\'oebryn_megamenu_titles\'] );?> !important;
}
.sticky {
    background-color: <?php echo esc_attr($checkOpt[\'oebryn_sticky_background\'][\'rgba\'] );?> !important;
    line-height: <?php echo esc_attr($checkOpt[\'oebryn_sticky_height\']); ?> !important;
    height: <?php echo esc_attr($checkOpt[\'oebryn_sticky_height\']); ?> !important;
    border-bottom: <?php echo esc_attr($checkOpt[\'oebryn_sticky_border\']); ?> solid <?php echo esc_attr($checkOpt[\'oebryn_sticky_border_color\'][\'rgba\']); ?> !important;
}
#header-bar.sticky ul#oebryn-navigation li a {
    color: <?php echo esc_attr($checkOpt[\'oebryn_sticky_font_color\'][\'rgba\']); ?> !important;
}
#header-bar.sticky ul.second-menu li.header-cart {
    border-bottom: <?php echo esc_attr($checkOpt[\'oebryn_sticky_border\']); ?> solid <?php echo esc_attr($checkOpt[\'oebryn_sticky_border_color\'][\'rgba\']); ?> !important;
    line-height: <?php echo esc_attr($checkOpt[\'oebryn_sticky_height\']); ?> !important;
    height: <?php echo esc_attr($checkOpt[\'oebryn_sticky_height\']); ?> !important;
}
.widget li, .widget li a , .contact-info-widget ul li > i {
    color: <?php echo esc_attr($footer_font_color); ?>;
}
.widget li a:hover {
    color: <?php echo esc_attr($footer_hovered); ?>;
}
footer .widget h6 {
    color: <?php echo esc_attr($footer_title); ?>;
}
.mobile-menu-logo-wrapper {
    background-image: url(\'<?php echo esc_url($checkOpt[\'oebryn_mobile_menu_background_image\'][\'url\']) ?>\');
    background-color: <?php echo esc_url($checkOpt[\'oebryn_mobile_menu_background\']); ?>;
}
.mobile-menu-logo-inner {
    background-color: <?php echo esc_attr($checkOpt[\'oebryn_mobile_menu_background_overlay\'][\'rgba\']); ?>;
}
.menu-parent, .mobile-menu-cart .mobile-menu-meta > p > a.menu-checkout {
    background-color: <?php echo esc_attr($checkOpt[\'oebryn_mobile_menu_background\'] );?> !important;
}
.ul.mobile-menu-cart-items li, .oebryn-mobile-menuwrapper li a, .mobile-menu-cart .mobile-menu-meta > p > a.menu-checkout {
    color: <?php echo esc_attr($checkOpt[\'oebryn_mobile_menu_fontcolor\']) ;?> !important;
}

2 个回复
最合适的回答,由SO网友:Isu 整理而成

你可以使用Hooks ,如果你想在头上打印一些样式,试试看

add_action(\'wp_head\', \'test_123\');

function test_123(){
    $options = get_option(\'option\');
    //Output it however you want :)
    ob_start();
    ?>
    <style type="text/css" id="someStyleInHead">
        body.home{
            font-size: <?php echo $options[\'fs_h\']; ?>;
        }
    </style>
    <?php
    $code = ob_get_clean();
    echo $code;
}
我希望它有帮助:)

SO网友:majick

不应尝试加载的原因wp-load.php 这是因为目录配置可能是非标准的,这将破坏主题CSS。(请注意,任何想要进入WordPress.Org存储库的主题或插件都必须删除这样的调用才能被允许。)

更好的方法是将CSS输出作为主题的函数,并通过AJAX回调加载它。是的,它会慢一点,但至少它不会有机会打破东西(好吧,除非你的WP_MEMORY_LIMIT 设置得太低了,即)例如。

// add the AJAX callback to ?action=bethaven_theme_css
add_action(\'wp_ajax_bethaven_theme_css\', \'bethaven_theme_css\');
add_action(\'wp_ajax_nopriv_bethaven_theme_css\', \'bethaven_theme_css\');
function bethaven_theme_css() {
    get_option(\'Bethaven\');
    // echo ...ALL YOUR CODE...
}

// enqeue the stylesheet via admin-ajax.php
add_action(\'wp_enqueue_styles\', \'bethaven_enqueue_stylesheet\');
function bethaven_enqueue_stylesheet() {
    $styleurl = admin_url(\'admin-ajax.php\');
    $styleurl = add_query_arg(\'action\', \'bethaven_theme_css\', $styleurl);
    // note fourth parameter will cachebust to show immediate style changes
    // (it would be better to have it as last theme settings saved time)
    wp_enqueue_style(\'bethaven-main\', $styleurl, array(), time());
}
否则,您也可以使用wp_add_inline_style 如果您不介意将CSS直接添加到页面内联中。

结束