0

I'm making a Wordpress website, and I have a question. Can I change CSS of certain elements with PHP DOM? I don't want to change structure of HTML, just few styles if certain conditions are met.

For example, if no images are present in my post I would like to add change color of the links in that post.

I'm thinking doing that with jQuery since it sounds a lot simpler, but I'm just wondering is this a valid way to do, or should I use PHP DOM?

3 Answers 3

1

For this type of operation, it is generally advised to do it with jQuery. This will save you processing time and power on your server for something easily done by the browser.

If, however, it absolutely must be done server-side, then you can achieve this with DOMAttr:

$attr = $element->setAttributeNode(new DOMAttr('style', 'border:1px solid black;'));
Sign up to request clarification or add additional context in comments.

1 Comment

I though so too, it's a lot simpler and faster solution, and I was just wondering is it the correct way to do or, or am I obliged to use PHP DOM.
0

You can try with something like this:

if(!$('img').length){
    $('a').css('color', 'red');
}

here a jsfiddle with the example

1 Comment

Thank you, I had similar idea in my mind, but I was wondering is using jQuery for editing styles when using Wordpress right way to do it, or should I focus on PHP DOM?
0

You can easily create an inline CSS block using PHP without PHP-DOM:

<?php if ($linkCount == 0) { ?>
    <style>
        a:link {
            color: red;
        }
    </style>
<?php } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.