Using CSS and PHP Together - php

I have built a Wordpress plugin that requires to me to add CSS styles through the plugin's PHP file.
Is there a better way of doing this? I can see this getting out of hand if not done properly.
function flags() {
<style type="text/css">
<?php if(get_option('display')=='Vertical') { ?>
<?php if (get_option('language_option')=='specific') { ?>
#flags {display:none !important; }
<?php } ?>
p.hello { font-size:12px; color:darkgray; }
<?php } ?>
</style>
<?php }
}

If there isn't much CSS, a better way is to add body classes that allow you to target your selectors only when they are present:
<body class="display-vertical">
...
And move the CSS from inline to the stylesheet:
.display-vertical #flags{
...
}

Related

WordPress - How to use PHP inside HTML injecting function?

I have a function that injects HTML code, the function looks like this:
function page_content() {
echo <<<HTML
<body>
<?php ?> //this won't work
</body
<style>
</style>
HTML;
}
Is there any way to write PHP code inside this HTML injection function?
This function is used for replacing the default WP Dashboard, but writing just HTML and CSS is limiting me totally :( Thanks
edit:
I have resolve this by breaking the HTML into two parts like this:
function page_content() {
echo <<<HTML
<body>
<div class="test">
<p>Body</p>
<p>Body</p>
<p>Body</p>
HTML;
echo do_shortcode('[contact-form-7 id="86" title="My Form"]');
echo <<<HTML
<p>Body</p>
<p>Body</p>
<p>Body</p>
</div>
</body>
<style>
.test {
display: flex;
flex-direction: column;
}
</style>
HTML;
}
If there is any better or easier way of doing this please let me know. I'm only learning, so always happy to learn the best ways of solving things in PHP. Thanks guys!
You can write like this.
<?php
function page_content() {
$output = 'HTML
<body>';
$output .= write php code here;
$output .=' </body
<style>
</style>
HTML';
echo output;
}
?>
I wrote like this so that you can understand easily but if you get used to combining HTML with php, you can just write together using .' and '. lol
<?php
function page_content() {
$output =
'HTML
<body>' . $write_anything_with_php .' </body>
<style>
</style>
HTML';
echo output;
}
?>

Display Wordpress enqueue style css files in inline?

Hi everybody I have this Wordpress website that I want to transform into an AMP website, one of the challnging task is to rewrite the css in inline.
So in the wordpress framework we have this functions.php file and inside we have the wp_enqueue_style function.
/**
* Proper way to enqueue scripts and styles
*/
function wpdocs_mytheme_styles() {
wp_enqueue_style( 'style', get_stylesheet_uri() . '/css/style.css' );}
add_action( 'wp_enqueue_scripts', 'wpdocs_mytheme_styles' );
Basically Wordpress will give us this line on out front end pages
<link rel='stylesheet' id='style' href='Path_to/css/stle.css' type='text/css' media='all' />
Can I change this display mechanism to make Wordpress display the registered styles in **INLINE this way :**
<style amp-custom>
<!-- The content of the style.css -->
<!-- .... -->
</style>
Yeah basically i have a lot of these files and I don't want to make a static change by opening each one and copy/pasting content n the header any idea?
You could do like this:
<style amp-custom>
{% include "/assets/css/main.min.css" %}
</style>
OR,
<style amp-custom>
/* any custom styles go here. */
body {
background-color: white;
}
amp-img {
border: 5px solid black;
}
amp-img.grey-placeholder {
background-color: grey;
}
</style>
Reference : AMP-DEV
You need to add the styles directly to the page head using wp_head()
add_action('wp_head', 'my_custom_styles', 100);
function my_custom_styles()
{
echo "<style>*{color: red}</style>";
}
is there any specific reason why you want to make use of inline styling?

Is there a way to mix CSS and PHP?

I'm working on a wordpress theme, and i want to have a custom header background. I used to use add_theme_support('custom-header); function that wordpress offers, it works but not the way i want it to. I want to have a background-image instead of an img. I made a function called header_background_callout() and i added a section, setting and control. the function works and outputs the background url that the user chooses through the customize section in wordpress. Is there a way that i can do something like this in css? : background-image: url(<?php echo wp_get_attachment_url(<?php get_theme_mod()); ?>);
Thanks in advance.
try this. echo " backgroud-img = 'url('get_theme_mod()')' ";
You can add inline styles to your theme using wp_add_inline_style:
<?php wp_add_inline_style( $handle, $data ); ?>
where $data is the CSS you want to include in your theme. This way you can have your php variables loaded as CSS for your website.
Yes kind off.
I made my css file a php file and because of that I can do as you say
background-image: url(<?php echo get_theme_mod(); ?>); // I suppose you need to echo the mod?
In the HTML you use it like;
<Link rel="stylesheet" type="text/css" href="style.php">
Edit you may need to change the header of the php file to a css file by:
<?php header("content-type: text/css; charset: UTF-8"); ?>
I found this easy way and it works the best:
In your Index.php file add:
<?php
function bg_img_alt() {
if(get_theme_mod('vibe_hero_background_setting') > 0) {
return wp_get_attachment_url(get_theme_mod('vibe_hero_background_setting'));
}else{
return get_template_directory_uri() . '/img/bg.jpeg';
}
}
?>
<style type="text/css">
background: linear-gradient(rgba(0,0,0,.3),rgba(0,0,0,.3)), url(<?php echo bg_img_alt(); ?>);
background-repeat: no-repeat;
background-position: center;
background-size: cover;
background-attachment: fixed;
</style>

Adding class to div only if user is logged in

I already asked this question, sry, but ill try again.
The question is: How do i bind css to a div, only if user is logged in?
I got following, and it is working(Also session startet and so on)
if (isset($_SESSION['username']))
{
include("/view/leftmenu.php");
}
Now i want to add 20% to margin-left on my maincontent. Maincontent comes after this.
So how do i activate some css to a div only when the user is logged in. PHP!!!!
Sry for asking again:(
<?php
$class = "";
if (isSet($_SESSION['username'])) {
$class = "addmargin";
}
?>
Then in your view/HTML/...
<div class="<?php print $class; ?>">...</div>
Now you just need something like this in your CSS
div.addmargin {
margin: 20%;
}
In the <head> do :
<?php if (isset($_SESSION['username'])) : ?>
<style type = "text/css">
#maincontent{
margin: 20%;
}
</style>
<!-- or add link to the style sheet -->
<link href="logged-in-style.css" rel="stylesheet" type="text/css">
<?php endif; ?>

Different CSS for homepage

Ok, based on the answer here by traitadmin (scroll down) I am trying to display different CSS for my front page and one for all other pages.
Previously, my body tag looked like this:
<body <?php body_class() ?>>
Now, I have edited it to look like this:
<body <?php if (is_home()) { ?> id="home"<?php } else { ?> body_class()<?php } ?> >
Also I have put new stylesheets in my enqueue, and all styles for the homepage now have #home in front of them.
However, there is no style at all now and my site broke down entirely, for all pages.
I think I may need body_class() also for the homepage. But how I can edit this code so that it works?
Easiest would actually be this:
<link rel="stylesheet" href="main.css">
<?php if (is_home()) {
echo '<link rel="stylesheet" href="home.css">'
} ?>
Rules in home.css should have a high priority than main.css. So with this if main.css has this:
body { background-color: white; }
and home.css has this:
body { background-color: blue; }
Since home.css is linked after main.css the rule in home.css will take priority and the background will be blue. This way you can only overwrite the styles you need to and all the other styles will still apply.
In your case after your other wp_register_style() just add an if test and then register another style.
if (is_home()) {
wp_register_style('home-style',get_stylesheet_directory_uri().'/stylesheets/home.css');
}
Looks like a wordpress code so here goes:
In the head:
<?php if (is_home()) {
// loading the stylesheet if it is the homepage
echo '<link rel="stylesheet" href="home.css">'
} else {
// loading the other stylesheet if it is not the homepage
echo '<link rel="stylesheet" href="other.css">'
} ?>
By default, WordPress adds two classes to the body tag: on the front page (is_front_page()) the home class is added, and on the home page (is_home()) the blog class is added. So, in your main CSS file, you can use the body class as follows:
#mydiv {background:green;}
.home #mydiv {background:blue;}
.blog #mydiv {background:red;}

Categories