Skip to main content
Commonmark migration
Source Link

##CSS customizable by manipulate parse_query##

CSS customizable by manipulate parse_query

###Similar approach with validate###

Similar approach with validate

##CSS customizable by manipulate parse_query##

###Similar approach with validate###

CSS customizable by manipulate parse_query

Similar approach with validate

deleted 6 characters in body
Source Link
Jevuska
  • 1.2k
  • 10
  • 21
/** Enqueue style for custom css
 *  we use parameter wpse_css and ( int ) 1 as value
 *
 */
add_action( 'wp_enqueue_scripts', 'wpse221867_enqueue_style', 99 );
function wpse221867_enqueue_style()
{
    /** check url ssl */
    $url = ( is_ssl() ) ?
            home_url( '/', 'https' ) : home_url( '/' );
    
    /** Register and enqueue wpse_style_php
     *  Build query with wpse_css as parameter, 1 as number to validation
     *
     */
    wp_register_style( 'wpse_style_php',
        add_query_arg( array(
            'wpse_css' => ( int ) 1
        ), $url ),
        array(), //your style handled
        null, //remove wp version
        'all' //media
    );
    wp_enqueue_style( 'wpse_style_php' );
}

/** Fire parse_request with function wpse221867_print_css
 *  Generate css in PHP
 *
 *  @return string CSS Content
 */
add_action( 'parse_request', 'wpse221867_print_css' );
function wpse221867_print_css()
{
    /** validate query on input */
    $css = filter_input( INPUT_GET, 'wpse_css', FILTER_VALIDATE_INT );
    
    if( empty( $css! )$css || ( int ) 1 != $css )
        return;
    
    ob_start();
    header( 'Content-type: text/css' );
    
    /** wpse_option_settings contain css in an array i.e
     *  array( 'wpse_css_content' => 'a{color:#ececec}' )
     *
     */
    $options = get_option( 'wpse_option_settings' );
    $raw_css = ( isset( $options['wpse_css_content'] ) )
                ? $options['wpse_css_content'] : '';
    
    /** sanitize for output */
    $content = wp_kses( $raw_css,
        array( '\'', '\"' )
    );
    $content = str_replace( '>', '>', $content );
    echo $content; // output
    die();
}
/** Enqueue style for custom css
 *  we use parameter wpse_css and ( int ) 1 as value
 *
 */
add_action( 'wp_enqueue_scripts', 'wpse221867_enqueue_style', 99 );
function wpse221867_enqueue_style()
{
    /** check url ssl */
    $url = ( is_ssl() ) ?
            home_url( '/', 'https' ) : home_url( '/' );
    
    /** Register and enqueue wpse_style_php
     *  Build query with wpse_css as parameter, 1 as number to validation
     *
     */
    wp_register_style( 'wpse_style_php',
        add_query_arg( array(
            'wpse_css' => ( int ) 1
        ), $url ),
        array(), //your style handled
        null, //remove wp version
        'all' //media
    );
    wp_enqueue_style( 'wpse_style_php' );
}

/** Fire parse_request with function wpse221867_print_css
 *  Generate css in PHP
 *
 *  @return string CSS Content
 */
add_action( 'parse_request', 'wpse221867_print_css' );
function wpse221867_print_css()
{
    /** validate query on input */
    $css = filter_input( INPUT_GET, 'wpse_css', FILTER_VALIDATE_INT );
    
    if( empty( $css ) || ( int ) 1 != $css )
        return;
    
    ob_start();
    header( 'Content-type: text/css' );
    
    /** wpse_option_settings contain css in an array i.e
     *  array( 'wpse_css_content' => 'a{color:#ececec}' )
     *
     */
    $options = get_option( 'wpse_option_settings' );
    $raw_css = ( isset( $options['wpse_css_content'] ) )
                ? $options['wpse_css_content'] : '';
    
    /** sanitize for output */
    $content = wp_kses( $raw_css,
        array( '\'', '\"' )
    );
    $content = str_replace( '>', '>', $content );
    echo $content; // output
    die();
}
/** Enqueue style for custom css
 *  we use parameter wpse_css and ( int ) 1 as value
 *
 */
add_action( 'wp_enqueue_scripts', 'wpse221867_enqueue_style', 99 );
function wpse221867_enqueue_style()
{
    /** check url ssl */
    $url = ( is_ssl() ) ?
            home_url( '/', 'https' ) : home_url( '/' );
    
    /** Register and enqueue wpse_style_php
     *  Build query with wpse_css as parameter, 1 as number to validation
     *
     */
    wp_register_style( 'wpse_style_php',
        add_query_arg( array(
            'wpse_css' => ( int ) 1
        ), $url ),
        array(), //your style handled
        null, //remove wp version
        'all' //media
    );
    wp_enqueue_style( 'wpse_style_php' );
}

/** Fire parse_request with function wpse221867_print_css
 *  Generate css in PHP
 *
 *  @return string CSS Content
 */
add_action( 'parse_request', 'wpse221867_print_css' );
function wpse221867_print_css()
{
    /** validate query on input */
    $css = filter_input( INPUT_GET, 'wpse_css', FILTER_VALIDATE_INT );
    
    if ( ! $css || ( int ) 1 != $css )
        return;
    
    ob_start();
    header( 'Content-type: text/css' );
    
    /** wpse_option_settings contain css in an array i.e
     *  array( 'wpse_css_content' => 'a{color:#ececec}' )
     *
     */
    $options = get_option( 'wpse_option_settings' );
    $raw_css = ( isset( $options['wpse_css_content'] ) )
                ? $options['wpse_css_content'] : '';
    
    /** sanitize for output */
    $content = wp_kses( $raw_css,
        array( '\'', '\"' )
    );
    $content = str_replace( '>', '>', $content );
    echo $content; // output
    die();
}
Additonal information
Source Link
Jevuska
  • 1.2k
  • 10
  • 21

##CSS customizable by manipulate parse_query##

@Scott Reinmuth, I think you misdirection about those tutorials, yes you use parse_request method.


###Similar approach with validate###

My similar approach with validate data. We just use file functions.php theme to add this code.

/** Enqueue style for custom css
 *  we use parameter wpse_css and ( int ) 1 as value
 *
 */
add_action( 'wp_enqueue_scripts', 'wpse221867_enqueue_style', 99 );
function wpse221867_enqueue_style()
{
    /** check url ssl */
    $url = ( is_ssl() ) ?
            home_url( '/', 'https' ) : home_url( '/' );
    
    /** Register and enqueue wpse_style_php
     *  Build query with wpse_css as parameter, 1 as number to validation
     *
     */
    wp_register_style( 'wpse_style_php',
        add_query_arg( array(
            'wpse_css' => ( int ) 1
        ), $url ),
        array(), //your style handled
        null, //remove wp version
        'all' //media
    );
    wp_enqueue_style( 'wpse_style_php' );
}

/** Fire parse_request with function wpse221867_print_css
 *  Generate css in PHP
 *
 *  @return string CSS Content
 */
add_action( 'parse_request', 'wpse221867_print_css' );
function wpse221867_print_css()
{
    /** validate query on input */
    $css = filter_input( INPUT_GET, 'wpse_css', FILTER_VALIDATE_INT );
    
    if( empty( $css ) || ( int ) 1 != $css )
        return;
    
    ob_start();
    header( 'Content-type: text/css' );
    
    /** wpse_option_settings contain css in an array i.e
     *  array( 'wpse_css_content' => 'a{color:#ececec}' )
     *
     */
    $options = get_option( 'wpse_option_settings' );
    $raw_css = ( isset( $options['wpse_css_content'] ) )
                ? $options['wpse_css_content'] : '';
    
    /** sanitize for output */
    $content = wp_kses( $raw_css,
        array( '\'', '\"' )
    );
    $content = str_replace( '>', '>', $content );
    echo $content; // output
    die();
}

IMHO, this method not recommended, especially for performance issue. Just use static css file, find another way to make it your css customizable, please take a look wp_add_inline_style ( example available ) and get_theme_mod.

@Scott Reinmuth, I think you misdirection about those tutorials, yes you use parse_request method.

IMHO, this method not recommended. Just use static css file, find another way to make it your css customizable.

##CSS customizable by manipulate parse_query##

@Scott Reinmuth, I think you misdirection about those tutorials, yes you use parse_request method.


###Similar approach with validate###

My similar approach with validate data. We just use file functions.php theme to add this code.

/** Enqueue style for custom css
 *  we use parameter wpse_css and ( int ) 1 as value
 *
 */
add_action( 'wp_enqueue_scripts', 'wpse221867_enqueue_style', 99 );
function wpse221867_enqueue_style()
{
    /** check url ssl */
    $url = ( is_ssl() ) ?
            home_url( '/', 'https' ) : home_url( '/' );
    
    /** Register and enqueue wpse_style_php
     *  Build query with wpse_css as parameter, 1 as number to validation
     *
     */
    wp_register_style( 'wpse_style_php',
        add_query_arg( array(
            'wpse_css' => ( int ) 1
        ), $url ),
        array(), //your style handled
        null, //remove wp version
        'all' //media
    );
    wp_enqueue_style( 'wpse_style_php' );
}

/** Fire parse_request with function wpse221867_print_css
 *  Generate css in PHP
 *
 *  @return string CSS Content
 */
add_action( 'parse_request', 'wpse221867_print_css' );
function wpse221867_print_css()
{
    /** validate query on input */
    $css = filter_input( INPUT_GET, 'wpse_css', FILTER_VALIDATE_INT );
    
    if( empty( $css ) || ( int ) 1 != $css )
        return;
    
    ob_start();
    header( 'Content-type: text/css' );
    
    /** wpse_option_settings contain css in an array i.e
     *  array( 'wpse_css_content' => 'a{color:#ececec}' )
     *
     */
    $options = get_option( 'wpse_option_settings' );
    $raw_css = ( isset( $options['wpse_css_content'] ) )
                ? $options['wpse_css_content'] : '';
    
    /** sanitize for output */
    $content = wp_kses( $raw_css,
        array( '\'', '\"' )
    );
    $content = str_replace( '>', '>', $content );
    echo $content; // output
    die();
}

IMHO, this method not recommended, especially for performance issue. Just use static css file, find another way to make it your css customizable, please take a look wp_add_inline_style ( example available ) and get_theme_mod.

Additonal information
Source Link
Jevuska
  • 1.2k
  • 10
  • 21
Loading
Improved Formatting
Source Link
Jevuska
  • 1.2k
  • 10
  • 21
Loading
added 3 characters in body
Source Link
Jevuska
  • 1.2k
  • 10
  • 21
Loading
Source Link
Jevuska
  • 1.2k
  • 10
  • 21
Loading