Question

I am developing theme to sell on themefores. I want to get option values in the head. but as the checkoptions would not be called yet i need to call wp-load.php. but the reviewer from themeforest said wp-load.php should not be called in any case. so can you please guide me how can i do the same without requiring wp-load.php ?

Here is my coede

<?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;
}
Was it helpful?

Solution

You could use Hooks , If you wish to print some styles in head just try

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;
}

I hope it help :)

OTHER TIPS

The reason you should not try to load wp-load.php like this is because the directory configuration may be non-standard and this will break the theme CSS. (Note also any theme or plugin wanting to be in the WordPress.Org repository will have to remove such a call to be allowed.)

A better approach would be to make the CSS output a function of the theme and load it via an AJAX callback instead. Yes it is a little slower, but at least it won't have a chance to break things (well, unless your WP_MEMORY_LIMIT is set way too low that is) eg.

// 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());
}

Otherwise, you can also use wp_add_inline_style if you don't mind the CSS being added directly into the page inline.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top