Is it possible to utilize a WordPress page as a CSS stylesheet?

Is there a way to customize my WordPress theme's CSS through a Page in the backend rather than editing the default style.css file? I've seen it as a page template before but I'm having trouble figuring it out on my own.

I'm currently running WordPress version 4.7.2 on a Multisite. My goal is to have the CSS generate when the theme is activated, which is why I want to use a Page for this purpose.

If there's an easy solution to this or any other methods you recommend, please let me know. Thank you!

Answer №1

If you're looking to enhance your website's design, I suggest utilizing a plugin like https://wordpress.org/plugins/wp-add-custom-css/ or leveraging the WordPress Customizer "Additional CSS" field.

Regarding the specific question at hand, creating a custom page template for CSS modification is relatively straightforward. To accomplish this, you'll need FTP access and a code editor to craft the template before transferring it to your theme folder (preferably within a child theme to avoid losing changes during updates).

The designated name for my CSS page template was: css-page.php

This template simply requires an opening php tag to function correctly.

Here's the code snippet for the css-page.php template:

<?php

/**
*
* Template Name: CSS page
* Not a recommended idea.
*
*/

header("Content-type: text/css; charset: UTF-8");

ob_start("compress");

//minify CSS
function compress( $minify ) {

    /* remove comments */
    $minify = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $minify );

    /* remove tabs, spaces, newlines, etc. */
    $minify = str_replace( array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $minify );

        return $minify;
}

//get the content
if (have_posts()) : 

while ( have_posts() ) : the_post();

    $content = get_the_content();

    $content = wp_filter_nohtml_kses( $content );

    echo $content;

endwhile;

endif; 

ob_end_flush();

To implement this custom CSS page template, add the following code snippet to your functions.php file in your child theme or a related custom plugin, ensuring to make the necessary adjustments as indicated:

Replace the value p='3134' with the respective page ID where the template will be applied, and modify or remove the id="something" attribute according to your preferences to facilitate any JavaScript manipulation.

Code for functions.php:

//Get CSS page contents
function myprefix_page_css() {
    echo '<link rel="stylesheet" id="something" href="'. site_url() .'/?p=3134" type="text/css" media="screen">';
}
add_action( 'wp_head', 'myprefix_page_css', 99 );

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble displaying CSS background images on hyperlinks

Hello everyone! I am in need of some assistance with a problem I'm facing. When I view my web page on my local computer, everything appears correctly with the images showing up. However, once I upload the web page to a server, the images no longer ap ...

Is it possible to stack navbar items in CSS?

I'm facing an issue with my navbar menu items overlapping on top of each other. https://i.sstatic.net/dEEpx.png What could be the reason behind this? Codepen Link: https://codepen.io/ogonzales/pen/mdeNNLB Code Snippet: <nav class="navbar navb ...

Incorporate RGBA backgrounds into various images simultaneously

I'm having trouble adding an rgba background to multiple images using Bootstrap while ensuring responsiveness. Unfortunately, something seems to have gone wrong. Here's an example of what I'm trying to achieve: https://i.sstatic.net/ueBEZ. ...

Exploring innovative CSS/Javascript techniques for creating intricate drawings

When using browsers other than Internet Explorer, the <canvas> element allows for advanced drawing. However, in IE, drawing with <div> elements can be slow for anything more than basic tasks. Is there a way to do basic drawing in IE 5+ using o ...

Python - Selenium: A guide to locating elements based on color

I have a situation where I need to select only the "Claim" button from the first div, even though both div cases are very similar. Is using background color a good way to identify my element? Or do you have any other ideas? 1. <div class="well well-sm ...

Ways to deactivate antd css-dev-only styles

I am currently working on a nextjs project using tailwind and antd. I am facing an issue where the antd Layout component styling is being applied and I am unable to remove it. :where(.css-dev-only-do-not-override-12upa3x).ant-layout-header This styling is ...

Dynamic website featuring fixed background with content transitioning through clicks or scrolls

Seeking a template or tutorial on how to create a website similar to the following examples: The desired website layout would allow users to scroll through content while keeping the background neutral, with the option to click links that transition to new ...

Is it possible for me to create a variable that is assigned the width of the page as its value?

As I work on building a website for my company, I encountered an issue where certain divs overlap when the page is resized smaller. I am looking for a solution to dynamically adjust the CSS using JavaScript based on the width of the page. Despite attempti ...

Clicking on text within a DIV using jQuery

How can I use jQuery to show an "alert" when text inside a DIV is clicked, excluding images and other tags? The texts are contained within various DIVs along with images and tags. Is there a way to achieve this using jQuery? Check out the jsFiddle example ...

What is the correct way to properly define the height of a page containing an angular-split area?

I am currently working on a page that utilizes angular-split. Here is a snippet of the code: <div class="height-max"> <app-nav-menu></app-nav-menu> <as-split direction="horizontal"> <as-split-area> <route ...

Having difficulty incorporating external SASS styles into my React.js project

In most of my projects, I follow a similar process for importing SASS styles. First, I create my react app and then install SASS using the command npm install node-sass. Next, I import my SASS file into my app components as shown below: import React from & ...

Ways to eliminate the dotted line from the image map in Internet Explorer 11

Below you will find the code I am working with: <img alt="Testing 2015" border="0" src="images/Test-2015.jpg" usemap="#Map" /> <p><map name="Map"><area coords="790,100,653,135" href="http://www.google.com/" shape="rect" style="cursor ...

What are the steps to creating a traffic light that operates automatically?

var RED = "#FF0000"; var YELLOW = "#FFFF00"; var GREEN = "#00FF00"; var DARK_RED = "#380000"; var DARK_YELLOW = "#383800"; var DARK_GREEN = "#003800"; var X_ALL = 150; var Y_RED = 100; var Y_Y ...

I cannot locate the dropdown list anywhere

As a beginner in html and css, I decided to follow a tutorial on youtube. The tutorial focuses on creating a navigation bar with dropdown menus using html and css. I wanted the names Ria, Kezia, and Gelia to be displayed when hovering my mouse over the Su ...

issues with alignment between bootstrap div and canvas

I am currently working on a project with a canvas inside a div using three.js. My goal is to display a purple bar for user settings on the right side of the three.js window. However, I am encountering an issue where Bootstrap is not recognizing that I want ...

Sign up with identical username and email on WordPress

I'm currently facing a challenge with implementing http://jsfiddle.net/67BUG/2/ in wordpress registration, but I have experimented with various approaches without success. Within the wp-login.php file, you can find the code here - http://pastebin.com ...

The webpage failed to show the hamburger menu

I'm trying to create a hamburger menu and have written the following code: <header class="header"> <nav class="flex flex-jc-sb flex-ai-c"> <a href="/" class="header__logo"> <img src="im ...

What is the best location for storing css files in Laravel?

I've come to realize that I can easily make changes to the app.scss file in my Laravel projects and see those changes reflected by running npm run dev or npm run watch. However, I'm faced with a dilemma when dealing with multiple .css files. Whe ...

Ways to eliminate line breaks and <br> tags in text using only CSS to create a button that trunc

I am currently working on incorporating a truncated text button using only CSS. The issue I'm facing is that the "show more" button doesn't ignore the line breaks or any other relevant HTML within the teaser and text body. This is causing too muc ...

What is the most effective way to display a pixelated background video in the header section of my website, with text overlaying it?

Currently immersed in an HTML and CSS project, I aim to showcase a captivating background video sourced from Pexel that occupies the entire top section of the screen above the "hello" and "meet the team" sections (excluding the navbar). While my Codepen li ...