How to add target=_blank automatically to PDF links using PHP? - wordpress

I'm wondering is it possible to automatically add target=_blank to any PDF link I have on my website, so that it will convert
link text
link text
to
link text
link text
as this site has a lot of PDFs, and it would be easier to do this automatically rather than have to set them all one-by-one.
I have tried the Javascript solution:
$(".newWindow a[href$='pdf']").attr('target','_blank');
But am wondering is there a way to do this automatically in PHP, perhaps by adding something to Wordpress's functions file?

If your .pdf links are only in your pages content. The fastest and easiest way to to do it is to replace inside your theme template file that shows your page content the following:
the_content();
by
echo str_replace('.pdf"', '.pdf" target="_blank"', get_the_content());
This will be inside the file content.phpof your theme.
Note that the function quoted above may have a parameter, (for example the_content('',FALSE,'');) you need to pass the same parameter in the get_the_content()function as well.

Using regex, you can perform smarter replacements. This example below will work on all <a> tags with a PDF extension in the URL, even if the URL has parameters.
echo preg_replace('/((<a (?=.*\.pdf)(?!.*target="_blank").*?)>)/', '$2 target="_blank">', get_the_content());
This regex performs two lookaheads: first, to check if the URL contains a PDF extension; and second, to check if the target="_blank" attribute is not already set. If both of those requirements are satisfied, then it will append the target="_blank" attribute to the end of the tag.

Related

Identify wordpress blocks

I am trying to edit a wordpress-theme (translations that are hardcoded in the theme etc) and stumble upon a problem here:
How do I identify, where a content block comes from?
Like, if my theme is constructed by 3 files post.php header.php and footer.php (which would make things pretty simple, but in my case it's quite some more files) and the content isn't set there consistently and the naming is neither "semantic", how could I identify that compiled block abc comes from header.php?
Is that something that can be done via Firebug/DevTools?
Although this question is specifically aiming at Wordpress as a system, the same problem occurs with other file-heavy systems like Typo3 or Magento. Isn't there some other routine then opening, searching all existing files?
Thanks for your answers
If your theme is translation-ready, you could try using Poedit. If not, you can identify where a block comes from knowing that:
Header.php: you'll find there all the strings that come in every page of your blog. Normaly Header.php includes all the code from the beginning of the HTML document until body, but it can be different from one theme to another. There may be diferrents headers, like header-gallery.php (for galleries) or similars.
Index.php: That's the index of your blog, normally it includes the code from the BODY of the html document for the index page.
Single.php, Page.php, Content.php, Aside.php or similars: They include the code for the body of every post/page type. For instance, single.php includes the BODY code for the single post. Sometiems, instead of single.php there are another options like content.php, content-wide.php or similars ("content-TypeOfPage.php"
Footer.php: It closes the HTML document for every post/page. Normaly it includes from the last lines before closing the BODY until the closing of the HTML tag.
Functions.php:: Take care editing this one. This file and the other files required/included on it include the code for the settings of the theme and any other functions implemented. For instance, the custom admin pages on the Dashboard are coded there.

is posible in wordpress post to replace a single word

I have on idea but i dont know if that is posible eg.
I'm writing a post in Wordpress site and I have to write a word too many times, is possible to write it once at the start and in the other part it to be written automatically by a shortcut or do I need a plugin. So if I change that word at the start all the other similar words to be changed automatically, like in word document action find and replace.
Yes, you can use a Shortcode for that. And you can write your own mini-plugin to do it.
The plugin would be simply:
<?php
/**
* Plugin Name: Single Word Shortcode
*/
add_shortcode( 'so', 'shortcode_so_22482571' );
function shortcode_so_22482571( $atts )
{
return "<a href='http://stackoverflow.com'>Stack Overflow</a>";
}
After activation, all [so] occurrences in your posts and pages will be converted to the Stack Overflow link.
You could write a shortcode, like brasoflio said, or you could just do exactly what you were thinking about in Word.... just do a find and replace in your document. Write the document in a text or markdown editor. Replace all instances of {abcdefg} with whatever text you want. Alternatively, you could use a find and replace plugin, or even write your own shortcode. But for something like this, I'd err on the side of laziness. Just write your text in a text editor, find and replace, then copy it in

Twitter share button doesn't forward custom text

I'm working on a website with twitter share option for each specific product.
I followed twitter API instructions for tweet-sharing, and everything works fine except custom display of text. For example I want user to tweet like this:
"What do you think? Should I buy this? http://url.etc #mywebsite"
but all I get when user tweets is the link:
http://url.etc
This is the code:
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
<a target="_blank" href="https://twitter.com/share" data-url="http://bit.ly/twitter-api-announce" data-via="testtest" data-text="What do you think? Should I buy this? " data-count="none" data-counturl="http://groups.google.com/group/twitter-api-announce" >TWITTER</a>
The problems seems to be with data-text option.
Any experience on this? Ideas?
Thanks
On Wordpress I just used Tweet
Works like a charm!
simply use a link like :
tweet
Just change what is between [] (and remove them)
note that everything have to be RFC (with weird chars such as 'space' replaced by %20 etc.)
twitter propose a nice page to make the buttons
https://about.twitter.com/resources/buttons#tweet
But my solution avoid the javascript to force the design of the buton
You can use this:
<a href='https://twitter.com/share?url=google.com&text=Signup>Tweet</a <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='//platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script>
Confirm that you have included the correct twitter scripts . Better still, generate your tweet button code from the twitter developer interface here ..
https://about.twitter.com/resources/buttons
If you would like to modify the tweetbutton content on the fly ...e.g after page load, you will have to creat and insert your tweet button into the html DOM dynamically .
Some guidance on that can be found here .
http://denvycom.com/blog/twitter-button-with-dynamic-custom-data-text-message/
Hopefully this is helpful.
You have to encode your text before inserting it in the link. The correct procedure is:
Encode the text with an online tool like this one
Put the result inside an HTML link (as suggested by #FenixAoras): Share on Twitter
If you generate your HTML with php, you can use the urlencode function directly in your script:
echo "Share on Twitter";
If you are using WordPress, the best way is creating a shortcode, beacuse with it you can use also the native functions of WordPress:
function tweet_this($atts, $content = null)
{
extract(shortcode_atts(array(
"text" => ''
), $atts));
return "<a href='https://twitter.com/intent/tweet?text=".urlencode( $text." - ".get_the_title()." - ".get_permalink() )."'>Share on Twitter</a>";
}
add_shortcode( 'tweet_this', 'tweet_this' );
(Note: the code above is just a lead, you can expand it and you have to test)
Usage:
[tweet_this text="my custom text with #hashtag and #Mention"]

Add HTML to node title in Drupal module, not in theme layer

I want to add some functionality to my Drupal 6 module: I want to insert an image next to certain node titles (it involves a span and an img tag). I thought I could do this by just implementing mymodule_preprocess_node() and modifying the title. However, Drupal strips out all tags to avoid XSS attacks.
I've seen many solutions that involve the theme layer (most commonly http://drupal.org/node/28537), but I want to do this in my module, not in the theme. I don't want to modify any .tpl.php files or template.php. Can anyone give me tips on how to do this?
You mention that you've tried preprocess_node(), and are correct that, if you are storing the img tag as part of the node title, Drupal will indeed strip that out, as it runs $node->title through check_plain in template_preprocess_node().
My suggestion would be to store the actual image as an image field (using some combination of the imagefield module and imagecache for sizing), set the display of that field to be hidden on the CCK display tab for the given content type, and then attach the image to be part of the $title variable in your module's preprocess function. This solution would also allow you to display that image next to the title in any views you may need to create.
By 'certain node titles' - do you mean all nodes titles from certain node types?
If so, you can likely style the node using only CSS. By default all nodes will have a class that corresponds to the node type. Using CSS background images, you can add an image to the node title.
Your module can call drupal_add_css and add in any required CSS into the page. This way, it is theme independent.
I think the easier way is with javascript / Jquery.
You create a Jquery script which is called only in certain types of nodes and pass the number of views from drupal to the jscript.
You can call drupal_add_js() inside your module_preprocess_node and pass a variable which contains the number of views or the image link to the script. Something like this:
<?php
drupal_add_js("var mymodule_imagelink = " . drupal_to_js($imagelink) . ";", 'inline');
drupal_add_js("my_js_file.js");
?>
then in my_js_file.js just change the text. There are several ways to acomplish this. For instance, you can do a search in the document and change the title to something else or you can use a determined ID, etc...
Find text string using jQuery?
http://api.jquery.com/replaceWith/

How to change tinyMCE editor's button's default tag output in wordpress?

Wordpress Tiny MCE editor and WP own editor both has button for <blockquote> . if we select any text and press this buttom then it wraps that text with <blockquote>.....</blockquote>.
I want to change this output to this
<blockquote><div class="quote_start"><div></div></div><div class="quote_end"><div></div></div>...................................</blockquote>
How can i do this manually or is there any wordpress plugin which can do the same?
I want to change behaviour of blockquote button in bot editor TinyMCE and WP own html editor?
I'm not sure you can use this to add that many divs but tinymce's valid elements config parameter does allow you to replace tags.
http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/valid_elements
For instance:
tinyMCE.init({
valid_elements : "blockquote/div[class=quote_start]"
});
Would replace all blockquote tags with a div with the quote_start class.
A better way might be to ignore tinymce here and write a filter for the functions.php file of your theme. http://codex.wordpress.org/Plugin_API/Filter_Reference/the_content. Find all the instances of blockquote and replace it with the code you want.
Maybe adding your own button could also be an option?
Some starting point could be this:
http://www.deliciousdays.com/tinymcebuttons/
and/or this:
http://wiki.moxiecode.com/index.php/TinyMCE:API/tinymce.Editor/addButton
hope it helps? Greetz, t..
If you want the same functionality in two different editors, you're probably better off writing (or looking for) a Wordpress filter that can replace the code. There's this one but it doesn't seem to be able to handle regular expressions (which you would need to replace HTML tags). Maybe this one can do what you need: Text Filter Suite
Getting both TinyMCE and Quicktags requires mods in two places. Here's how to do the Quicktags:
http://website-in-a-weekend.net/extending-wordpress/diy-wordpress-unraveling-quicktags-wordpress-plugins/

Resources