Remove everything but certain link from element using PHP - php

I'm creating a simple website that collates all of my tweets into a "wall" using a WP plugin. The problem is though that the entire body of the tweet (Links, text, hashtags, etc.) is pulled into the_content whereas I only want to display the a particular chunk of content.
Is there a way that I can completely remove everything else from an element using PHP?
This is my example markup;
<div class="element">
<div class="overlay">
<p>#username This is some plain text. http://www.t.co/xxxxxxx #twitterhashtag</p>
</div>
</div>
I want to remove everything apart from the #username. Is that possible?

Try this
<?php $content = get_the_content();
$pieces = explode("</a>", $content);
<div class="element">
<div class="overlay">
<p>echo $pieces[0]."</a>";</p>
</div>
</div>

Related

Is is possible to get WPBakery generated rows as a variable?

I'm creating custom elements in WPBakery (formerly Visual Composer), but encountering issues with its rendered markup.
For example:
I've created a custom element called text. text currently spans 12 columns in the WP admin:
The rendered markup for this looks like:
<div class="row justify-content-center">
<div class="text">
<div class="text-left container">
<h2>This is header</h2>
<p>This is content</p>
</div>
</div>
<div class="col-sm-12">
<div class="vc_column-inner">
<div class="wpb_wrapper"></div>
</div>
</div>
</div>
As you can see, it's creating an empty col-sm-12 div and not even applying the column styles to text.
Ideally what I want to do is get the column WPBakery is generating and place it in a variable.
This way, I can do something along the lines of:
<div class="text <?php echo $column; ?>">
I think this will be handy when a user eventually decides they want two text blocks next to each other:
So each text block will be col-6.
To summarise, I'm trying to figure out why Bakery is outputting an empty div with columns rather than to wrap around my text block? As I'm not sure why this is happening, the only solution I can think of is to get the column data as a variable and then echo is in the text block.

Drupal Paragraphs module - remove extra markup

I would like to remove the extra markup that is around the generated paragraph items.
<h1>
<div class="field field-name-field-title field-type-text field-label-hidden">
<div class="field-items">
<div class="field-item even">Title</div>
</div>
</div>
</h1>
I would like it to display like this:
<h1>Title</h1>
I’ve not had any luck trying some of the suggestions in the Paragraphs issues https://www.drupal.org/node/2251909 such as editing the following files:
paragraphs-item.tpl.php
paragraphs-items.tpl.php
paragraphs.theme.inc
Does anyone have any experience with this module and be able to offer some assistance?
You can use strip_tags() to remove the html tags.
<h1><?php print strip_tags(render($content['YOUR_FIELD'])); ?></h1>
or
<?php print strip_tags(render($content['YOUR_FIELD']), "<h1>"); ?>
Hope this helps you.

How to manage links in an automatic way?

In my project's homepage there must be some images, these must link to other pages. Now, I want to manage these links in an automatic way using PHP. The only idea I came up with is to make a form and insert the images into some buttons and make their background invisible.
<div class="container">
<div class="row">
<div class="col-md-4">
<h3>FRESH ALBUMS</h3>
</div>
</div>
<div id="myline"></div>
<div class="row">
<div class="col-md-3">
<img src="contentimg/abbeyroad.jpg" height="200px" width="200px">
<div class="caption">
<h4>Beatles</h4>
</div>
<p> test</p>
</div>
<div class="col-md-3">TEST2</div>
<div class="col-md-3">TEST3</div>
<div class="col-md-3">TEST4</div>
</div>
</div>
This is the HTML at the moment. I just want to know if there are better ideas, options and if it will stay responsive. Thanks in advance.
I do not understand exactly what you are asking, but I cannot leave comments so I will try to answer what I think you are asking.
For the HTML part, you don't need a form with buttons, you can make hyperlinks out of the images instead, and the PHP can get the images to match the hyperlinks when it sends the page to your users browser. The PHP code would look like:
echo "
<a href='".$newPageLink."'>
<img src='".$imageLocation."' width='100px' height='100px'>
</a>";
Where $newPageLink is a variable in your PHP script that holds the path information for your hyperlink, and $imageLocation is the path information for where the image is stored.
All you need is a foreach or while loop to go through all of the images that you want on the page and also all of the pages that each image should link to. If it is always a manageable and unchanging list, you could store the values in an array in your script. For larger lists, or lists that are dynamic/always changing you might need to use a MySQL database table to store the image and hyperlink information.

I want to remove certain parent- and child-divs in all my wordpress posts with php or some other script

Is there a quick way, via script maybe, to remove a certain pair of div's out of all my wordpress posts? For example:
I want to go from this:
<div class="single_textimage">
<div class="youtube_play"><iframe src="-,-"></iframe></div>
<div class="single_textimage_text">Some text.</div>
<div class="single_textimage_copyright">Some text.</div>
</div>
To this:
<div class="youtube_play"><iframe src="-,-"></iframe></div>
AND
From this:
<div class="single_textimage">
<img class="aligncenter size-full wp-image-1700" src="-,-" />
<div class="single_textimage_text">Some text.</div>
<div class="single_textimage_copyright">Some text.</div>
</div>
To this:
<img class="aligncenter size-full wp-image-1700" src="-,-" />
So I want the divs: single_textimage, single_textimage_text and single_textimage_copyright to go.
I hope there is an easy script, or difficult for that matter. Via "php", "mysql" or "jquery" for example, that I can put in test.php in the root or something...
I hope I supplied you with enough information. If I haven't made myself clear enough, please reply. :)
Seems to me like you should be able to take those out of whatever template your using - probably in a PHP include, but I don't really use WordPress, so I wouldn't know where without seeing all your files. If you're bent on using jQuery instead of modifying the template, I would throw in some CSS too, to hide the elements that will be removed:
.single_textimage, .single_textimage_text, .single_textimage_copyright{
display:none;
}
Then you can take the elements you want to keep out of their parent DIVs, and place them right after (or before):
$('.youtube_play, .wp-image-1700').each(function(){
$(this).parent().after($(this));
});
Then you can remove the elements you don't want from the page:
$('.single_textimage, .single_textimage_text, .single_textimage_copyright').remove();
Here's a fiddle: https://jsfiddle.net/3uztorzL/
I would use this search and replace utility to update all of the content in the DB:
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
You'll need a regex to replace <div class="single_textimage_text">Some text.</div> (assuming the "some text" is different in each post). The utility supports regex replace. This may do it:
<div class="single_textimage_text">(.*?)</div>
Make sure you make a backup before you do the replace.

Wordpress adding strange html code, breaking some page formatting

I am getting this oddball extra <br> inserted into this page above the "Affiliate" title/section. http://londoncapital.biz/affiliate-program/
Using either Firebug or Element Inspector, I still cannot determine where this is coming from.
This is the generated HTML.
<div class="entry clearfix">
<div class="two_thirds"></div>
<div class="one_third last" style="text-align: justify;"></div>
<hr class="clear">
<p></p>
<div class="one_third"></div>
<p></p>
<p></p>
<div class="one_third"></div>
<br> (THIS SEEMS TO BE THE OFFENDING ELEMENT I THINK)
<div class="one_third last"></div>
<div class="clear"></div>
<br>
I have gone over the page coding in WP page itself and and all three of those sections are identical.
What am I missing here? Some errant code in some php file?
UPDATE INFO: This is the actual code from the WordPress page: http://pastebin.com/pwJbAGdg
I can't paste the whole code here because I'll be here forever trying to clean up the formatting from the paste.
Surely using this stuff in editor is really cool but if you REALLY need it you can just remove spaces, copy this stuff:
<div class="entry clearfix"><div class="two_thirds"></div><div class="one_third last" style="text-align: justify;"></div><hr class="clear"><div class="one_third"></div><div class="one_third"></div><div class="one_third last"></div><div class="clear"></div><br>
Open Wordpress Super editor and go to TEXT mode - !important
Paste it - should work.
But that's not the best coding sample :)

Categories