wp_update_custom_css_post

Update the `custom_css` post for a given theme.

Description

(WP_Post|WP_Error) wp_update_custom_css_post( (string) $css, (array) $args = array() ); 

Inserts a custom_css post when one doesn't yet exist.

Returns (WP_Post|WP_Error)

Post on success, error on failure.

Parameters (2)

0. $css (string)
CSS, stored in post_content..
1. $args — Optional. (array) => array()
Args.

Options

  • preprocessed (string) => ''

    Pre-processed CSS, stored in post_content_filtered.. Normally empty string. Optional.

array(

    /**
     * Pre-processed CSS, stored in `post_content_filtered`. Normally empty string. Optional.
     *
     * @type string
     * @default ''
     * @optional
     */
    'preprocessed' => ''
);        


Usage

  1. if ( !function_exists( 'wp_update_custom_css_post' ) ) { 
  2. require_once ABSPATH . WPINC . '/theme.php'; 
  3.  
  4. // CSS, stored in `post_content`. 
  5. $css = ''; 
  6.  
  7. // Args. 
  8. $args = array( 
  9. 'preprocessed' => '' 
  10. ); 
  11.  
  12. // NOTICE! Understand what this does before running. 
  13. $result = wp_update_custom_css_post($css, $args); 
  14.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/theme.php  
  1. function wp_update_custom_css_post( $css, $args = array() ) { 
  2. $args = wp_parse_args( $args, array( 
  3. 'preprocessed' => '',  
  4. 'stylesheet' => get_stylesheet(),  
  5. ) ); 
  6.  
  7. $data = array( 
  8. 'css' => $css,  
  9. 'preprocessed' => $args['preprocessed'],  
  10. ); 
  11.  
  12. /** 
  13. * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated. 
  14. * This filter can be used by plugin that offer CSS pre-processors, to store the original 
  15. * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`. 
  16. * When used in this way, the `post_content_filtered` should be supplied as the setting value 
  17. * instead of `post_content` via a the `customize_value_custom_css` filter, for example: 
  18. * <code> 
  19. * add_filter( 'customize_value_custom_css', function( $value, $setting ) { 
  20. * $post = wp_get_custom_css_post( $setting->stylesheet ); 
  21. * if ( $post && ! empty( $post->post_content_filtered ) ) { 
  22. * $css = $post->post_content_filtered; 
  23. * } 
  24. * return $css; 
  25. * }, 10, 2 ); 
  26. * </code> 
  27. * @since 4.7.0 
  28. * @param array $data { 
  29. * Custom CSS data. 
  30. * @type string $css CSS stored in `post_content`. 
  31. * @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string. 
  32. * } 
  33. * @param array $args { 
  34. * The args passed into `wp_update_custom_css_post()` merged with defaults. 
  35. * @type string $css The original CSS passed in to be updated. 
  36. * @type string $preprocessed The original preprocessed CSS passed in to be updated. 
  37. * @type string $stylesheet The stylesheet (theme) being updated. 
  38. * } 
  39. */ 
  40. $data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) ); 
  41.  
  42. $post_data = array( 
  43. 'post_title' => $args['stylesheet'],  
  44. 'post_name' => sanitize_title( $args['stylesheet'] ),  
  45. 'post_type' => 'custom_css',  
  46. 'post_status' => 'publish',  
  47. 'post_content' => $data['css'],  
  48. 'post_content_filtered' => $data['preprocessed'],  
  49. ); 
  50.  
  51. // Update post if it already exists, otherwise create a new one. 
  52. $post = wp_get_custom_css_post( $args['stylesheet'] ); 
  53. if ( $post ) { 
  54. $post_data['ID'] = $post->ID; 
  55. $r = wp_update_post( wp_slash( $post_data ), true ); 
  56. } else { 
  57. $r = wp_insert_post( wp_slash( $post_data ), true ); 
  58.  
  59. if ( ! is_wp_error( $r ) ) { 
  60. if ( get_stylesheet() === $args['stylesheet'] ) { 
  61. set_theme_mod( 'custom_css_post_id', $r ); 
  62.  
  63. // Trigger creation of a revision. This should be removed once #30854 is resolved. 
  64. if ( 0 === count( wp_get_post_revisions( $r ) ) ) { 
  65.  
  66. if ( is_wp_error( $r ) ) { 
  67. return $r; 
  68. return get_post( $r );