Adding a separator after each article in Wordpress - html

I need to use an image to separate my articles (posts). I found this function below
function funny_cat_filter( $content ) {
$content .= '<img src="(image url)"/>';
return $content;
}
add_filter( 'the_content', 'funny_cat_filter' );
Don't mind the function name. This works just fine but with one little pesky detail. It adds image on pages also and I don't want that. I want this only to be added after each post not on every single page.
How do I do that?

You can check if you are in a post with is_single() or in the home with is_home().
function funny_cat_filter( $content ) {
if (is_single() || is_home())
$content .= '<img src="(image url)"/>';
return $content;
}
add_filter( 'the_content', 'funny_cat_filter' );

Related

How to add HTML Tag and class inside Link Tag in Post - Wordpress

I'd like to add classes and an html tag inside a link tag of Post so I linked a word to one of my wordpress page which look like this :
contact
and I want when I link it to be like :
<span>contact</span>
because my theme use span to do a hover feature.
if possible only in Post (Blog part), there is a class which all posts has it's : post-contents
I know I can modify in the HTML Code of my post but I want it to be universal so as soon as I add a link in a post (even a new one) class and span tag are added
Any thought?
This should do the trick;
function wrap_anchor_text_with_span( $content ) {
if ( ! is_admin() && preg_match( '~<a(.*?)>(.*?)</a>~', $content ) ) {
$content = preg_replace_callback( '~<a(.*?)>(.*?)</a>~', '_add_span', $content );
}
return $content;
}
add_filter('the_content', 'wrap_anchor_text_with_span', 10);
function _add_span( $matches ) {
if ( ! ( $title = strip_tags( $matches[2] ) ) ) { // If we only have an image inside the anchor
return '<a' . $matches[1] . '>' . $matches[2] . '</a>';
} else {
return '<a' . $matches[1] . '><span data-title="' . esc_attr( $title ) . '">' . $matches[2] . '</span></a>';
}
}
What this function does is that it hooks to the_content filter and places a span inside all anchor tags.
Note that if the anchor contains an image, a span won't be added. As per another answer from another thread, Nikola Ivanov Nikolov
Then use a filter to handle the anchors...
add_filter('the_content', 'addClassToLinks');
function addClassToLinks($content){
return str_replace( '<a ', "<a class='myclass'", $content);
}
Hope this helps.
Regards,
-B.

Add default code in wordpress post

I want to add a simple code to all new post by default
I tried to used this code in .function.php (wpbeginner)
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "If you like this post, then please consider retweeting it or sharing it on Facebook.";
return $content;
}
I am using plugin named Shortcodes Ultimate
Code:
[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]
I tried to normal use simple html and CSS code but it show the same error for both cases
https://i.stack.imgur.com/wgAs4.png
Try this -: wrap your code is a single quote as you are already using double quotes.
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = '[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]';
return $content;
}

Parse img from RSS-feed using PHP SIMPLE HTML DOM Parser

I am trying to parse this site (to get the img-link): http://statigr.am/feed/parishilton
This is my code:
include 'parse/simple_html_dom.php';
// Create DOM from URL or file
$html = file_get_html('http://statigr.am/feed/parishilton/');
// Find all images
foreach($html->find('img') as $element)
{
echo $element->src . '<br>';
}
The script doesn't return anything! Why is that ? I want the img link.
It's because all images are inside CDATA section and parser ignores it, so the solution is
$html = file_get_html('http://statigr.am/feed/parishilton/');
$html = str_replace("<![CDATA[","",$html); // clean-up
$html = str_replace("]]>","",$html); // clean-up
$html = str_get_html($html); // re-construct the dom object
// Loop
foreach($html->find('item description img') as $el)
{
echo $el->src . "<br />";
}
Replace all CDATA from the returned content and then use str_get_html to create DOM object from that string and loop through the images. (Tested and works).
Output :
http://distilleryimage3.s3.amazonaws.com/cc25d8562c9611e3a8b922000a1f8ac2_8.jpg
http://distilleryimage7.s3.amazonaws.com/4d8e22da2c8911e3a6a022000ae81e78_8.jpg
http://distilleryimage5.s3.amazonaws.com/ce6aa38a2be711e391ae22000ae9112d_8.jpg
http://distilleryimage3.s3.amazonaws.com/d64ab4c42bc811e39cbd22000a1fafdb_8.jpg
......
......

Overriding Drupal 7 Theme functions

I am new to Drupal as well as Drupal theme development. After learning how to override drupal template, I wanted to know how to override drupal 7 theme function. From the node Using the theme layer, the following things I have learned :
For every hook in a module, we need to register a hook function
Im plementation is 'template' file if it is defined in return array of the function. Otherwiaw implementation is through function.
Now following the book A definative guide to Drupal 7, to override a theme function
1. Copy - paste the theme function to my theme's template.php file
2. Change the beginning of filename from theme_ to yourtheme_
3. Save
To give example, it overrides the following function :
function theme_more_link($variables) {
return '<div class="more-link">' . l(t('More'), $variables['url'], array('attributes' => array('title' => $variables['title']))) . '</div>';
}
Now if I want to override the theme function comment_help() in comment.module:
function comment_help($path, $arg) {
switch ($path) {
case 'admin/help#comment':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Comment module allows users to comment on site content, set commenting defaults and permissions, and moderate comments. For more information, see the online handbook entry for Comment module.', array('#comment' => 'http://drupal.org/documentation/modules/comment/')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Default and custom settings') . '</dt>';
$output .= '<dd>' . t("Each <a href='#content-type'>content type</a> can have its own default comment settings configured as: <em>Open</em> to allow new comments, <em>Hidden</em> to hide existing comments and prevent new comments, or <em>Closed</em> to view existing comments, but prevent new comments. These defaults will apply to all new content created (changes to the settings on existing content must be done manually). Other comment settings can also be customized per content type, and can be overridden for any given item of content. When a comment has no replies, it remains editable by its author, as long as the author has a user account and is logged in.", array('#content-type' => url('admin/structure/types'))) . '</dd>';
$output .= '<dt>' . t('Comment approval') . '</dt>';
$output .= '<dd>' . t("Comments from users who have the <em>Skip comment approval</em> permission are published immediately. All other comments are placed in the <a href='#comment-approval'>Unapproved comments</a> queue, until a user who has permission to <em>Administer comments</em> publishes or deletes them. Published comments can be bulk managed on the <a href='#admin-comment'>Published comments</a> administration page.", array('#comment-approval' => url('admin/content/comment/approval'), '#admin-comment' => url('admin/content/comment'))) . '</dd>';
$output .= '</dl>';
return $output;
}
}
How can I do it? Its name does not starts from theme_.
The core comment module is uging hook_help function. You have to use the same function in your custom module. Notice that you should first clean any $output data using $output = "";

Output post for second wordpress editor

I recently added a new editor to all my pages and posts admin area with the Wordpress 3.3 new feature to do this
add_action( 'edit_page_form', 'my_second_editor' );
function my_second_editor() {
// get and set $content somehow...
wp_editor( $content, 'mysecondeditor' );
}
My question is how do I output what I enter in this second editor on my website/page? Do I need to make a custom loop? The codex is not very helpful unfortunately.
Thanks
You'll need get_post_meta(), use it like:
echo get_post_meta(get_the_id(), 'mysecondeditor');
Read more: http://codex.wordpress.org/Function_Reference/get_post_meta
To save the data entered in your second editor you'll need this code in your functions.php file:
add_action( 'save_post', 'save_post', 10, 2 );
function save_post( $post_id, $post ) {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
update_post_meta( $post_id, 'mysecondeditor', stripslashes( $_POST['mysecondeditor'] ) );
}
So after that he's the full code for your second editor:
wp_editor( get_post_meta(get_the_id(), 'mysecondeditor', true), 'mysecondeditor' );
The true above makes sure only a single variable is returned and not an array so you can use it right away.
user2019515I's (Apr 18 '13 at 12:06) answer works for me, I can add text and gallery, but when I display that with this code:
<?php echo get_post_meta(get_the_ID(),'mysecondeditor')['0']; ?>
I got gallery code instead of the images, so:
mytext [gallery ids="102,62"]
How could I display the text (mytext) and the images too?