How to add style to a php snippet - php

In my wordpress site I have
<?php wp_loginout(); ?>
which produces
Log in
I want to add the following style to the a tag
style="color:white;"
I don't know php. Would I try find the php and then add it somehow? And how would I add it to the php?

Use below code:-
<div id='anchorStyle'> <!-- define div -->
<?php wp_loginout(); ?>
</div>
CSS
#anchorStyle a{
color:white;
}
Hope it will help you :)

wp_loginout() has a filter hook 'loginout' which you could use to influence the output.
Add a filter function to functions.php of your theme:
add_filter('loginout', 'loginout_selector');
function loginout_selector($text) {
$selector = 'class="woo-sc-button"';
$text = str_replace('<a ', '<a '.$selector, $text);
return $text;
}
Ref: http://codex.wordpress.org/Function_Reference/wp_loginout

In functions.php replace/add this filter
add_filter( 'wp_nav_menu_secondary_items','wpsites_loginout_menu_link' );
function wpsites_loginout_menu_link( $menu ) {
$loginout = '<li class="myNewClass">' . wp_loginout($_SERVER['REQUEST_URI'], false ) . '</li>';
$menu .= $loginout;
return $menu;
}
fore reference: http://codex.wordpress.org/Function_Reference/wp_loginout

you can't style the php. The a tag is the tag you should style (html).
if you want to add your style to the a tag best practice is to find/create the file which has extension .css or create a file called (name).css
Simple way to style all your a tags is
a {
color: white;
}
Again, this styles all a tags and I don't know the entire code.
If you would want to specify which a tag gets which colors. Read up on CSS and adding class/id or nested elements.

Related

Adding an ::after pseudo-element to HTML Generated in PHP

I currently have HTML generated by PHP which looks like this:
echo '<div class="right">';
echo '<p class = "triangle-isosceles left">'; the_sub_field("right_quote", false); echo '::after'; echo '</p>';
echo '</div>';
Which currently displays in html as:
<p>"This is the output of the_sub_field function ::after"</p>
When I want it to output as
<p>"This is the output of the_sub_field function" ::after</p>
a ::after or ::before are a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML).
While the end result is not actually in the DOM, it appears on the page as if it is, and would essentially be like this:
div::after {
content: "hi";
}
So to add a pseudo you must use css, try this :
echo '<div class="right">';
echo '<p class = "triangle-isosceles left">'; the_sub_field("right_quote",
false); echo '</p>';.
echo '<style>p.triangle-isosceles:after{content:"something"}</style>';
echo '</div>';
then the result would be like :
p.triangle-isosceles:after{
content:" -> after content";
}
<p class="triangle-isosceles">"This is the output of the_sub_field function"</p>
You cannot add an :after element inside your DOM (your current loaded page) you have to add it to your stylesheet file on your <p> element using his classes.
like this
.triangle-isosceles:after{
content:'';
}
See this : https://www.w3schools.com/cssref/sel_after.asp
Of course you can use the php function fopen and fwrite and fclose to add your new css rules.
<php?
// This is your after element you put in a variable (like a storage place)
$addMyAfterElement = ".triangle-isosceles:after{ content:'';";
// This is your stylesheet file you open and you put in a variable
$myStyleSheet = fopen("styles\style.css", 'a+');
// This is writting your after element inside the stylesheet file
fwrite($myStyleSheet, $addMyAfterElement);
// And here you close your stylesheet file
fclose($myStyleSheet);
?>

Zend Framework: changing color of string in view

I want to set color for specific string in ZendFrame work, is it the best way?
View:
.not-found{color : red;}
<?php
echo '<div class="not-found">';
echo $this->not_found;
echo '</div>';
?>
Controller:
$this->view->not_found='no result';
Yes this is correct just wrap your css code within css html markup
If you want your css to work it needs to be in a css file linked to the view or in a style like :
<style>
.not-found{color : red;}
</style>

How to put an image in this table

im having a problem and i can't seem to put an image icon on my delete and edit button i've put a div class and styled it in my css but still the image doesn't show.what might be wrong here?
HTML
<?php
$this->table->set_heading("Name","Nationality","Contact Number","Number of Guest","Date of Arrival","Time","Pickup Location","Package","Other Request","Delete Record","Edit Record");
$qry = $this->db->like('name',$search_key)->order_by('date')->get('booking');
if ($qry->num_rows > 0) {
foreach ($qry->result() as $row) {
$this->table->add_row(anchor('site/print_records/'.$row->id, $row->name, 'target="_blank"'),$row->nationality,$row->contactnum,$row->number_of_guest,$row->date,$row->time,$row->pickup,$row->package,$row->request,anchor('site/delete/'.$row->id, "<div class='deleteico'></div>),anchor('site/update/'.$row->id, "<div class='editico'></div>));
}
}
else{
echo "No records found!";
}
echo $this->table->generate();
?>
CSS
#deleteico{
background-image: url('../pictures/delete_icon.png');
}
#editico{
background-image: url('../pictures/edit_icon.png');
}
You are using class in your code and are applying the css to an id selector.
If you want to use classes, change your css to the following:
.deleteico{
background-image: url('../pictures/delete_icon.png');
}
.editico{
background-image: url('../pictures/edit_icon.png');
}
# is used to select IDs, and . is used for classes.
Note: and as #skip405 has mentioned as well, you will need to set a width and height in your css or the div will be 0x0 big and thus not show anything.
Try adding a space inside your brakets:
you need to add width and height of the background-image to the #deleteico and #editico ids. Then add to them and change it to id from class in your code.
For instance,
<div id='deleteico'> </div>
<div id='editico'> </div>
Hope this helps.

PHP in CSS - "If page is in this directory, echo this"

I have several sites on the same hosting package. They’re all in different directories. ( i.e. “htdocs/site1”). I want to be able to have them all share one CSS file.
I was wondering if there is a way to change the color of certain elements based on which directory the site is in.
Ideally I would like to be able to define what directory the page is in and what color to use for each directory. Then in my CSS do something like:
.button { color: <?php echo $color ?> ;}
to each element that gets a color change.
Is this possible and if so, how do I go about setting this up?
thank you
You could add different classes to your body tag depending on the directory:
<body class="<?php echo $dir; ?>">
where the $dir variable is given a different value (let's say $dir = 'site1',...) for each directory...
... And then have something like:
.site1 #button { /*styles*/ }
.site2 #button { /*styles*/ }
.site3 #button { /*styles*/ }
in your CSS file.
You could add a CSS class to the body tag of the HTML document to determine the site. In PHP you would have to find a way to write the correct site into to the document. Do you use some kind of global template?
Just to give you an idea:
PHP:
<?php
// some code
// some logic to determine which site you are on - let's say ...
$site = 'SITE1';
?><body class="<?php echo $site; ?>"><?php
// more code
?>
CSS:
body.SITE1 #button { color: #ff0000; }
body.SITE2 #button { color: #0000ff; }
body.SITE2 #button { color: #123456; }
You could dynamically generate the css file using php, where you'd have
<?php
switch ($_SERVER['SERVER_NAME']) {
case 'www.site1.com':
$color = '#ff0000';
break;
case 'www.site2.com':
$color = '...';
break;
...
default:
$color = '...';
?>
.someclass { color : <?php echo $color ?>; }
This is somewhat inefficient, however. You'd be building a css file just to change a single color each time. Better way is to simply embed the color change in the page's header as an in-line style. That way you don't have to mess with making your server parse CSS files as if they were PHP scripts, and you can put the site-specific css overrides into that inline style in the site's header.
Honestly, I would suggest you add a class to your html tag:
<html class="site1">
And within your CSS, define your css:
.site1 * .button1{ background:#f00;}
.site2 * .button1{ background:#f0f;}
.site3 * .button1{ background:#ff0;}
You can find some more information on this subject here for a PHP approach.

How do I highlight text in PHP with conditions?

I have some numbers, and depending on the number's value I need to highlight a string which is in a table. The table is not in database.
Does anyone know how to highlight text in php?
PHP won't "highlight" text, that is done with CSS/HTML. You only use PHP to output the correct HTML.
Something like this:
$my_num = 9;
foreach ($my_array_of_numbers as $num)
{
// See if $num matches $my_num, if so - add the "highlight"
// class to the HTML element we're using
$css_class = ($num == $my_num) ? 'highlight' : '';
echo '<td class="'.$css_class.'">'.$num.'</td>';
}
Then in your CSS:
.highlight {
background:yellow;
}
You may also want to consider using javascript for this, which can handle the task after the HTML has already been generated.
Presentation, like highlighting, is done in CSS, not PHP.
My advice would be to create a CSS class which has a background-color defined to be used as highlighting. Then, in PHP, you can conditionally wrap your string in <span> tags which have that class, so the text would be highlighted.
CSS:
.highlight {
background-color: #FF0;
}
PHP:
if (highlightCondition) {
echo '<span class="highlight">' . $string . '</span>';
} else {
echo $string;
}

Categories