Is there an easy way to automatically wrap any h2 element in the div class "entry-content" in another div class "entry-header"
So the end result would look something like:
<div class="entry-content">
<div class="entry-header">
<h2>Some Title</h2>
</div>
</div>
I assume this can be done with PHP, but I'm not sure. Thanks for any input!
In terms of wordpress I would probably verge towards creating a shortcode such as
[header]Some Title[/header]
I would make the shortcode take the content and wrap the given code around it.
See some documentation here: http://codex.wordpress.org/Shortcode_API
ugly one:
$content = str_replace('<div class="entry-content">', '<div class="entry-content"><div class="entry-header">', $content);
$content = str_replace('</h2>', '</h2></div>', $content);
Can you do it in prototype ? This would be my easy solution:
var div = $('entry-content');
$(div).insert ({'top' : '<div class="entry-header">'} );
$(div).insert ({'bottom' : '</div>'} );
Maybe I'm missing somethig :)
Related
I want to remove a div added by a plugin to the content of WordPress posts. So the post has this structure:
<div class="post">
<div class="some-class">
<p>content</p>
</div>
</div>
I want to remove <div class="some-class"> and its closing </div> but leave the content. So it would be:
<div class="post">
<p>content</p>
</div>
using this filter:
add_filter( 'the_content', 'remove_class' , 100 );
function remove_class( $content ) {
$content = preg_replace('#<div[^>]*class="some-class"[^>]*>.*?</div>#is', '', $content);
return $content;
}
the content is also deleted, I just want the div and the closing div to be deleted. Any idea how?
this question is not duplicate of the other question because I want to remove a specific div not just all divs
You could just try to remove class attribute, so that only <div> is left, using code like this:
add_filter( 'the_content', 'remove_class' , 100 );
function remove_class( $content ) {
$content = preg_replace('/class=".*?"/', '', $content);
return $content;
}
#user7592255 you can try with jQuery like this:
$('p').unwrap();
If you can set an id or class on the p element you can target it more accurately
The content is removed because you replace the entire matched string with an empty string. Use a subpattern to capture the content of the <div> element and use it as replacement:
$content = preg_replace(
'#<div[^>]*class="some-class"[^>]*>(.*?)</div>#is',
'$1',
$content
);
However, be aware that it won't work properly if the content of <div class="some-class"> contains a <div> element.
There is no way to parse HTML using regex. The correct solution is to use an HTML parser (DOMDocument f.e.) to parse the HTML fragment and create its DOM, then operate the changes on the DOM and render it back to HTML.
I'm trying to split a section of HTML into an array of 2 values to wrap around a template. I am trying to avoid using a placeholder but wondered if there was some way of performing something similar to the jQuery wrap() function.
So this is the code that I want to wrap:
<img src="/resources/img/photo.png">
This is the portion of HTML that I wish to wrap the image above:
<div class="container"><div class="col-md-6"></div></div>
So that the end result will be:
<div class="container">
<div class="col-md-6">
<img src="/resources/img/photo.png">
</div>
</div>
The only way that I can currently think of doing it is with a placeholder like so but would like to not have to use this method:
<div class="container"><div class="col-md-6">[REPLACE_ME]</div></div>
Any help you can give me on this would be much appreciated!
It seems to me that you pretty much have to use a placeholder if you're going to try to do this using just string manipulation. Otherwise, PHP won't really know where to put the contents within the wrapper. The [REPLACE_ME] could be done using %s in a format string for printf or sprintf.
$contents = '<img src="/resources/img/photo.png">';
$wrapper = '<div class="container"><div class="col-md-6">%s</div></div>';
// [REPLACE_ME]^
$result = sprintf($wrapper, $contents);
Here try this:
<?php
$img = '<img src="/resources/img/photo.png">';
echo '<div class="container"><div class="col-md-6">'.$img.'</div></div>';
In PHP, if you want something to show up on a page, you need to use echo. You then can concatenate a string using ..
I'm trying to add a thumbail to a div using the_post_thumbnail() function.
The code I'm using is the following:
<div class="sc-col sc-s12 sc-m3 sc-card sc-card-basic">
<figure>'.the_post_thumbnail(array('370', '188')).'</figure>
</div>
But when the code is rendered in a browser the code becomes (just took a part of the rendered code):
<img width="370" height="182" src="/wp-content/uploads/2016/05/image.png">
<div class="sc-col sc-s12 sc-m3 sc-card sc-card-basic">
<figure></figure>
</div>
As you can see the image is rendered outside the div and figure. My question is why is it doing this? Because now my whole layout is breaking now and, the problem is that I have to use that function else I would have used an other function. Can I somehow rewrite the function so it only returns the src link using add_filter function?
https://developer.wordpress.org/reference/functions/the_post_thumbnail/ shows that the function the_post_thumbnail is designed to actually output something to the page, not return a string.
So change your code to something more like this:
$html = '<div class="sc-col sc-s12 sc-m3 sc-card sc-card-basic">
<figure>';
echo $html;
the_post_thumbnail(array('370', '188'))
$html = '</figure>
</div>';
echo $html;
This solved it for me:
<figure><?php the_post_thumbnail(array('370', '188')); ?></figure>
As staded by #ChrisLear the_post_thumbail() doens't return a string and there for fails.
I need to replace a HTML section identified by a tag id in a source code, which is combination of HTML and PHP using PHP. In case it's pure HTML, DOM parser could be used; in case there is no DIV in DIV, I can imagine how to use preg_match. This is what I am trying to do - I have a code (loaded into a string) like:
<div>
<img >
</div>
<? include(); ?>
<div id="mydiv">
<div>
<div>
<img >
</div>
</div>
</div>
and my task is to replace content of "mydiv" DIV with a new one e.g.
<div id="newdiv>
some text
</div>
so the string will look like this after the change:
<div>
<img >
</div>
<? include(); ?>
<div id="mydiv">
<div id="newdiv>
some text
</div>
</div>
I have already tried:
1) parsing the code using DOMdocument's loadHTML => it produces a lot of errors in case PHP code is included.
2) I played around a bit with regexes like preg_match_all('/<div id="myid"([^<]*)<\/div>/', $src, $matches), which fails in case more child divs are included.
The best approach I have found so far is:
1) find id="mydiv" string
2) search for '<' and '>' chars and count them like '<'=1 and '>'=-1 (not exactly, but it gives the idea)
3) once I get sum == 0 I should be on position of the closing tag, so I know, which portion string I should exchange
This is quite "heavy" solution, which can stop working in some cases, where the code is different (e.g. onpage PHP code contains the chars as well instead of just simple "include"). So I am looking so some better solution.
You could try something like this:
$file = 'filename.php';
$content = file_get_contents($file);
$array_one = explode( '<div id="mydiv">' , $content );
$my_div_content = explode("</div>" , $array_one[1] )[0];
Or use preg_match like you said:
preg_match('/<div id="mydiv"(.*?)<\/div>/s', $content, $matches)
Yes there is. First you need to use a function that will get the content of the file. Lets call the file homepage.php:
$homepageString = file_get_contents('homepage.php');
Now you have a string with all the content. The next thing you would do is use the preg_replace() function to take out the part of code that you want to take out:
$newHomepageString = preg_replace('/id="mydiv"/',"", $homepageString);
Now you overwrite the existing homepage.php file with the new source code:
file_put_contents("homepage.php", $newHomepageString);
Let me know if it worked for you! :)
hey guys,
my php variable $content holds html!
i want to filter this $content for
[q=SomeQuestoin] and [a=SomeAnswer]
and wrap each match inside of a div.question and div.answer.
So whenever this [q=Some Question][a=Some Answer] structure is found in $content i want to put out this.
<div class="qanda">
<div class="question">
Some Question
</div>
<div class="answer">
Some Answer
</div>
</div>
Is that possible? Important is that the Qustion Text or the Answer Text could hold html tags as well. like <p> or <b> etc.
update:
$q_regex = '/\[q=([^"]+?)]/is';
$q_output = '<div class="qanda"><div class="queston">$1</div>';
$content = preg_replace($q_regex, $q_output, $content);
$a_regex = '/\[a=([^"]+?)]/is';
$a_output = '<div class="answer">$1</div></div>';
$content = preg_replace($a_regex, $a_output, $content);
http://www.spotlesswebdesign.com/blog.php?id=12
tutorial on using regex to do bbcode parsing. people would recommend using a bbcode parser module however. should be safe to regex since you are not using nesting and whatnot.
EDIT
possible but tricky. could be error prone. something like this maybe:
$result = preg_replace('/\[q=(.+?)].+?\[a=(.+?)]/is', '<div class="qanda"><div class="question">$1</div><div class="answer">$2</div></div>', $subject);