Skip to content

Instantly share code, notes, and snippets.

@ovizii
ovizii / functions.php
Last active January 7, 2018 22:50
Create discreet widget - only shows if not empty
//add discreet text widget
add_action('widgets_init', 'custom_discreet_text_widget');
function custom_discreet_text_widget() {
class DiscreetTextWidget extends WP_Widget_Text {
function DiscreetTextWidget() {
$widget_ops = array('classname' => 'discreet_text_widget', 'description' => __('Arbitrary text or HTML, only shown if not empty'));
$control_ops = array('width' => 400, 'height' => 350);
$this->WP_Widget('discrete_text', __('Discreet Text'), $widget_ops, $control_ops);
}
@ovizii
ovizii / functions.php
Last active July 13, 2020 18:17
Remote site snapshot using Wordpress API shortcode
function wpr_snap($atts, $content = null) {
extract(shortcode_atts(array(
"snap" => 'http://s.wordpress.com/mshots/v1/',
"url" => 'http://www.sagive.co.il',
"alt" => 'My image',
"w" => '400', // width
"h" => '300' // height
), $atts));
$img = '<img src="' . $snap . '' . urlencode($url) . '?w=' . $w . '&h=' . $h . '" alt="' . $alt . '"/>';
@ovizii
ovizii / functions.php
Created September 3, 2015 07:29
Get a custom field value through shortcodes
add_shortcode('field', 'shortcode_field');
function shortcode_field($atts){
extract(shortcode_atts(array(
'post_id' => NULL,
), $atts));
if(!isset($atts[0])) return;
$field = esc_attr($atts[0]);
@ovizii
ovizii / functions.php
Created May 4, 2015 18:28
Manipulate the excerpt
//removing and improving the excerpt
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
/*improve the excerpt, keep HTML in there*/
function improved_trim_excerpt($text) {
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
@ovizii
ovizii / functions.php
Created June 22, 2014 16:44
Restrict home page posts to certain categories
function my_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '11');
}
}
add_action( 'pre_get_posts', 'my_home_category' );
//If you would like to include more than one category, then just add another ID by inserting a comma and the ID number. For example, this is will display category 11 and category 14.
//$query->set( 'cat', ’11, 14’ );
@ovizii
ovizii / new_gist_file.js
Created May 12, 2014 19:57
Add this javascript to add QR code as a widget
//Paste this code into a text widget to put a qr code on each page.
<script type="text/javascript">
var url=encodeURIComponent(window.location);
var qr="<img style=\"maxwidth:100%\" src=\"https://chart.googleapis.com/chart?chs=240x240&cht=qr&chl="+url+"&choe=UTF-8\"/>";
document.open();
document.write(qr);
document.close();
</script>
@ovizii
ovizii / functions.php
Created April 19, 2014 09:35
Change email/user for WP emails
add_filter( 'wp_mail_from', 'my_mail_from' );
function my_mail_from( $email )
{
return "change-this-to-your-email-address";
}
add_filter( 'wp_mail_from_name', 'my_mail_from_name' );
function my_mail_from_name( $name )
{
return "My Name";
@ovizii
ovizii / functions.php
Created April 19, 2014 09:34
Send mails through SMTP without plugin
add_action('phpmailer_init','send_smtp_email');
function send_smtp_email( $phpmailer )
{
// Define that we are sending with SMTP
$phpmailer->isSMTP();
// The hostname of the mail server
$phpmailer->Host = "smtp.example.com";
// Use SMTP authentication (true|false)
@ovizii
ovizii / functions.php
Created April 12, 2014 22:27
Stop tripping line breaks
function clear_br($content){
return str_replace("<br />","<br clear='none'/>", $content);
}
add_filter('the_content', 'clear_br');
@ovizii
ovizii / functions.php
Created April 12, 2014 22:03
Character count for content/excerpt
/* letter counter */
add_action( 'admin_head-post.php', 'letter_count_js'));
add_action( 'admin_head-post-new.php', 'letter_count_js' ));
public function letter_count_js(){ echo '<script>jQuery(document).ready(function(){ jQuery("#postexcerpt .handlediv").after("<input type=\'text\' value=\'0\' maxlength=\'3\' size=\'3\' id=\'excerpt_counter\' readonly=\'\' style=\'background:#fff; position:absolute;top:0.2em;right:2em; color:#666;\'>"); jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length); jQuery("#excerpt").keyup( function() {jQuery("#excerpt_counter").val(jQuery("#excerpt").val().length); }); jQuery("#wp-word-count").after("<td id=\'wp-character-count\'>Character count: <span class=\'character-count\'>0</span></td>"); jQuery("#wp-character-count .character-count").html(jQuery("#wp-content-wrap .wp-editor-area").val().length); jQuery("#wp-content-wrap .wp-editor-area").keyup( function() {jQuery("#wp-character-count .character-count").html(jQuery("#wp-content-wrap .wp-editor-area").val().length