Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all 8245 articles
Browse latest View live

thescribbler on "(FUNCTION) map_meta_cap() question."

$
0
0

Hi everyone, I have a quick question about a snippet of code in the map_meta_cap() function.

The code is:

case 'edit_user':
	case 'edit_users':
		// Allow user to edit itself
		if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] )
			break;

		// If multisite these caps are allowed only for super admins.
		if ( is_multisite() && !is_super_admin( $user_id ) )
			$caps[] = 'do_not_allow';
		else
			$caps[] = 'edit_users'; // edit_user maps to edit_users.
		break;

I'm confused about the break that occurs directly after if ( 'edit_user' == $cap && isset( $args[0] ) && $user_id == $args[0] ).

From what I can see, the conditional checks if the current user's ID is the same as the ID of the user they wish to edit. If it's true, you would expect it to map 'edit_user' to edit_users' (i.e. they can edit themselves), but the code leads it on to a break instead, i.e. edit_user will NOT be mapped to edit_users if the condition is true.

Is this not the opposite of what it's supposed to do? Using current_user_can() shows that it works the correct way (and commenting out the break reverses the functionality), but I can't understand how it's working!

I know it's minor but it's driving me mad! I realise that I must be reading the code incorrectly, so any insight would be greatly appreciated. Cheers.


webdev2087 on "Overwrite taxonomy and post type"

$
0
0

My theme came with a custom taxonomy post type.

I want to rename the original and duplicate it with another different name.

I tried finding everything relative in functions.php and copy it over to the child theme assuming it would override the parent theme.

Upon refreshing the site it went blank.

Any ideas?

Should I copy the whole functions.php and use that in my child theme. Or is it possible to override?

or maybe it broke the template (even though I was not on a relative page) trying to call that which I changed?

Thank you in advance. I know it's probably really simple I just don't know enough about WP yet.

odie2 on "Stretch too short image for image size"

$
0
0

Hello,

I have own: add_image_size( 'featured-image', 1200, 335, true );

However, I am migrating from old Joomla and have images with max width 800px.
My post image in full-width page is exactly 1200x335px. I am using max-height: 335px; for div parent, so it's nicely responsive.

Problem is if I'll upload for example 800x600px 'featured-image' is created in 800x335px with hard crop. My css stretch it to 1200px with width: 100%, but I have additional image areas on top and bottom of image (800px -> 1200px: 1,5 so 335px * 1,5 = 502,5px).

I can't force div parent for height: 335px; so I could vertical center image by this, because responsive max-height: 335px;.

I need to WordPress creates 1200x335px or 800x223px (height - 335px / 1,5), so then css will stretch it right.

JSFiddle (without JS) - unexpected result two issues: higher in expected in zoom-in and bad placed in zoom-out.

My all known ways to find solution - used.
Thanks in advance!
Greetings

Aapie on "hacked my Titel/description and now mobile is broken"

$
0
0

Played around with adding attributes to reposition the Header and Description on my site and got what I wanted in Safari, Chrome and iPad, but iPhone Safari display is bad and I'm concerned Android as well. Could someone look at my child theme code and give me some suggestions - before my hack it was displaying OK, but the header image was truncated.
http://www.coyof1.com

robertritz on "(FUNCTION) Programmatically set thumbnail via URL - IF ELSEIF issue"

$
0
0

I am attempting to set the featured thumbnail programmatically via functions.php. Here is the code I half wrote (the rest comes very helpfully from another StackExchange post. There don't seem to be any syntax errors (I've got debugging turned on) but when making a new post with the correct category nothing happens. I'm very new to PHP so I'm assuming it's an issue with my code but I'm not sure exactly where.

I'm loading the HTML of a URL in a custom field via DOM, then pulling the IMG tags. After that an If ElseIf looks at the category and grabs the nth image based on the category and attaches it as the post thumbnail. I feel that the logic is right but I might be missing something.

<?php
add_action('publish_post', 'auto_featured_image_publish_post');
function auto_featured_image_publish_post($post, $post_id) {

	$category = $post->the_category;

	// we're using the excerpt field, change this to whatever field
    // you're using
    $post = get_post($post_id);
    $htmlURL = $post->original_guid;

    // try to load the webpage
    $doc = new DOMDocument();
    $doc->loadHTMLFile($htmlURL);

	// get all image tags
    $images = $doc->getElementsByTagName('img');
	if ( has_post_thumbnail($post_id) == false ) {
		if ($category == "Mongolia.GoGo.mn") {
			$imgcount = count($images);
			$imageURL = $imgcount[62]->getAttribute('src');

			// download image from url
			$tmp = download_url($imageURL);

			$file = array(
				'name' => basename($imageURL),
				'tmp_name' => $tmp
			);

			// create attachment from the uploaded image
			$attachment_id = media_handle_sideload( $file, $post_id );

			// set the featured image
			update_post_meta($post_id, '_thumbnail_id', $attachment_id);
		} elseif ($category == "News.mn English") {
				$imgcount = count($images);
				$imageURL = $imgcount[2]->getAttribute('src');

				// download image from url
				$tmp = download_url($imageURL);

				$file = array(
					'name' => basename($imageURL),
					'tmp_name' => $tmp
				);

				// create attachment from the uploaded image
				$attachment_id = media_handle_sideload( $file, $post_id );

				// set the featured image
				update_post_meta($post_id, '_thumbnail_id', $attachment_id);
		} elseif ($category == "InfoMongolia") {
				$imgcount = count($images);
				$imageshortURL = $imgcount[3]->getAttribute('src');
				$imageURL = "http://infomongolia.com/" . $imageshortURL;

				// download image from url
				$tmp = download_url($imageURL);

				$file = array(
					'name' => basename($imageURL),
					'tmp_name' => $tmp
				);

				// create attachment from the uploaded image
				$attachment_id = media_handle_sideload( $file, $post_id );

				// set the featured image
				update_post_meta($post_id, '_thumbnail_id', $attachment_id);
		}
	}
}

Bloodspencer on "Instagram API - Dynamic Redirect Uri"

$
0
0

Hi guys,

i´m working on an instagram feed plugin for wordpress. This plugin will shows the own instagram feed with many options.

The problem: to get the access token i have to declare a static redirect url in my code and on the instagram settings page. Both must be excact the same..so the problem begins :D People who are using this plugin have another url than my redirect url...and doesn´t work..

Would be awesome, if you have a solution for this thing. I heard about state parameters..maybe would be this the answer?

Thanks!

adaldesign on "meta_query 'compare' =>'IN' not working as described in Codex"

$
0
0

I'm at a loss, trying to use what seemed like a rather textbook meta_query parameter as one of the arguments in a get_users() request.

I formatted the code (displayed below) using the instructions provided on the get_users Codex page, as well as the WP_Meta_Query Codex page referenced.
There is also a post on StackOverflow that suggests this same syntax should work just fine.

However it's not working for me. I am able to get results using the 'LIKE' compare key but that one can't process an array of values like 'IN' is supposed to. I could try to hack my way around this with an ugly foreach loop but I'd really rather keep it kosher.

Here's my code:

$user_query_args = array(
	'role'         => '',
	'meta_query' => array(
		array(
			'key' => 'guest_type',
			'value' => $gest_type // this works fine
		),
		array(
			'key' => 'wp_'. $blog_ID .'_registration_status',
			'value' => $registration_status // this works fine
		),
		array(
			'key' => 'mandatory_tracks',
			'value' => 'newphy',
			'compare' => 'IN'  //*** but this don't work ***
		)

	),
	'orderby'      => 'login',
	'fields'       => 'ID'
);

$reported_users_IDs = get_users( $user_query_args );

And just so you know I'm not crazy, this row exists in the user_meta table of the database:

82 3 mandatory_tracks a:2:{i:0;s:6:"newphy";i:1;s:9:"workforce";}

Finally, you can see my work in progress with some var_dumps here.

Any help is greatly appreciated!

Guido on "Plugin pagination doesn't work"

$
0
0

Hi,

I have created an events plugin and want to include pagination.
But somehow I cannot make it work.

I have wrapped my loop in a shortcode function and this is what I have so far:

$events = new WP_Query( $args );
if ( $events->have_posts() ) :
	while( $events->have_posts() ): $events->the_post();
		echo '<div id="wrapper">';
			echo '<h4>';
			echo the_title();
			echo '</h4>';
			the_content();
		echo '</div>';
	endwhile;

	next_posts_link( 'Older posts' );
	previous_posts_link( 'Newer posts' );

	else:
		echo '<p>';
		_e('There are no upcoming events.');
		echo '</p>';
endif;

Works fine, correct number of posts set in Settings > Reading is displayed, but no pagination visible. So what am I missing here?

Guido


legion_x on "How can i detect if a widget is in sidebar or included in the template"

$
0
0

Hello,

I have a temaplte where i use some plugin offering a widget features. Now i use this widget once in sidebar an once directly included into the template via "the_widget();".

Now i want to modify them. The sidebar widget should just look different than the widget i included directly. It's more than just some toying around with css.

Is there any way like conditional tags to detect if the wigdet is placed within the sidebar or not?
How can i differ if its in sidebar or directly in code?

Thank you i advance, Chris

csem on "child theme devlopment wordpress genesis"

$
0
0

How many files are must required for child theme development in wordpress genesis.

Funkphenomenon on "Sidebar nav: Show children and siblings when on 2nd or lower levels"

$
0
0

For this question I tried about 10-15 scripts and 5 plugins, but all fail when you're on a grandchild page.

For a website with sometimes 4 levels of pages I would like the sidebar to show:

When on a 1st level page:

  • Current page as menu heading
  • Children of the current (1st level) page.

When on a 2nd level page:

  • Current page as menu heading
  • Siblings of the current page, and the current page itself.
  • (If they exist) Children of the current (2nd level page), but not the children of 2nd level siblings

When on the 3rd level (grandchildren):

  • Current page as menu heading
  • Siblings of the current page, and the current page itself.
  • (If they exist) Children of the current (3nd level page), but not the children of 3nd level siblings

A plugin called Flexi Pages Widget has been doing this perfectly for the past 4 years, but it conflicts with WPML and Flexi Pages hasn't been updated since 2013, so that's why I'm looking for an alternative.

The majority of alternatives do not show the grandchildren on my site, even when the solution indicates that it should. Some plugins that didn't work for me:

I also have a bunch of other scripts I got from blogs and Stack Overflow topics, but the sources below looked the most promising. However, theset still fail at displaying children when you are at the 2nd level (i.e. 2nd level doesn't show grandchildren):

With the site I'm working on, most if not all solutions for 3 levels or more (incl. all plugins) keep showing the 2nd level siblings when at 2nd level or 3rd level.
When at a 1st level page, Esmi's solution shows the whole page tree, including grandchildren. So, it is able to determine that it has grandchildren. But when you go to the 2nd level, the tree is trimmed to 2nd level siblings, while I'd like to see the current page's children, too. And maybe even it's grandchildren.

I'm on WP 4.1.1 with theme Hybrid, and I paste the code in a PHP Code sidebar widget, and if necessary in functions.php.

Can anybody tell me how to show the current page's children, siblings and ancestors in the sidebar, for sites with 3 levels or more?

Arevico on "Modifying Main Query Single Page Request"

$
0
0

Hi al,,

I've a question regarding implementing the following functionality. If a specific page is requested, i want to switch the content based on some option set in the admin screen.

My main idea was to use pre_get_posts to change this behaviour. However,the documentatation explicitly lists: 'pre_get_posts should not be used to alter the query for single Page requests'. Now, my main question is: which hook should I use to override the WP_Query object pointing to the new page?

Guido on "Display today's events too"

$
0
0

Hi,

I use this to display my events, but somehow today's events are not being displayed at all. Only future events. Did test on several servers.

$meta_query = array(
	'relation' => 'AND',
	array(
		'key' => 'event-date',
		'value' => time(),
		'compare' => '>='
	)
);

$query_args = array(
	'post_type' => 'event',
	'meta_key' => 'event-date',
	'orderby' => 'meta_value_num',
	'order' => 'asc',
	'meta_query' => $meta_query,
);

$events = new WP_Query( $query_args );

I use both 'time' and 'compare >=' but don't understand why today's events are not being displayed. Who can help me?

Guido

tobbger on "Edit "Attachment Display Settings""

$
0
0

I would like to edit the code that's inserted when selecting "Embed Media Player" as the "Attachment Display Settings" when uploading a song to a blog post.

Right now the code that's inserted is:
[audio mp3="song url"][/audio]

I would like to add a text and a link before the actual audio player. The song URL should be inserted as a direct link and then as an audio player. The text for the link should be based on the post title ("Artist - Song title"). The code would look like this:

<strong>MP3:</strong> <a href="song url">Artist - Song title</a>
[audio mp3="song url"][/audio]

How would I do this the easiest way?

belette on "Unusual behavior after modifying a php file"

$
0
0

I am used to modifying and testing php pages. But this time...

In header.php of Vantage Premium theme, I have placed just before to display a menu of my own. Works great.
In this lemenu.php, after displaying my menu, I have placed:

to display another line of menu if we are on page 123, which is a special page for members. Works great!

BUT...

for testing purpose, and because I have to change the menu lemenumembres.php, I removed the if ( is_single('123')) php block and... the second menu still displays. I have checked the files (header.php, lemenu.php) thru the WP editor, thru FTP, even thru CPANEL file manager and the block is effectively removed. But the content of lemenumembres.php still displays.
I have even removed from header.php and the first menu stops displaying but... the second one still does.

Of course, I have emptied my browser's cache many times ans I even tried with different browsers who never accessed this page. Still the same.

Can someone tell me what I dont understand?


Please Add Screenshots on "simple = - / x calculations with 2 custom fields"

$
0
0

simple = - / x calculations with 2 custom fields

Would anyone have a simple piece of code that calculates and displays a simple mathematical calculation (=-/x) between 2 custom fields?

Thanks heaps

EsferaTextual on "Wordpress Hacked yesterday"

yiags987 on "Add a REPEATABLE metabox to my plugin code"

$
0
0

I am creating a plugin called BANDS for my personal site which basically sits under PAGES and has added metaboxes to add band details such as videos, soundcloud & crucially Festival Appearances.

I have now created the plugin and it works really well for what I need. I have a new menu item called 'bands' for all my pages and the metaboxes in admin I require for the extra information.

I have one or 2 small issues left - 1 which is really vexing me! As you will see in the code below - I have one section called "Festival Appearances" which has a field for the festival name & then then the date of the show. I want to make this field a REPEATABLE field so the user can click a "add another +" link and get a new meta box to enter a 2nd festival appearance.

I have seen plenty of code to do it but am having a real hard time mixing it with what I have.

Along the way I wouldn't mind making the date field a 'date picker' rather than the standard text area if anyone know how?

Any help would be greatly appreciated! Code below:

// Registers the new post type and taxonomy

function wpt_bands_posttype() {
	register_post_type( 'bands',
		array(
			'labels' => array(
				'name' => __( 'Bands' ),
				'singular_name' => __( 'Band' ),
				'add_new' => __( 'Add New Band' ),
				'add_new_item' => __( 'Add New Band' ),
				'edit_item' => __( 'Edit Band' ),
				'new_item' => __( 'Add New Band' ),
				'view_item' => __( 'View Band' ),
				'search_items' => __( 'Search Band' ),
				'not_found' => __( 'No bands found' ),
				'not_found_in_trash' => __( 'No bands found in trash' )
			),
			'public' => true,
			'supports' => array( 'title', 'editor', 'thumbnail', 'comments' ),
			'capability_type' => 'post',
			'rewrite' => array("slug" => "bands"), // Permalinks format
			'menu_position' => 5,
			'menu_icon' => plugins_url( 'icon.png', __FILE__ ),
			'register_meta_box_cb' => 'add_bands_metaboxes'
		)
	);
}

add_action( 'init', 'wpt_bands_posttype' );
add_action( 'add_meta_boxes', 'add_bands_metaboxes' );

// Add the Bands Meta Boxes

function add_bands_metaboxes() {
	add_meta_box('wpt_bands_details', 'Band Details', 'wpt_bands_details', 'bands', 'normal', 'default');
}

function wpt_bands_details() {
	global $post;

	// Noncename needed to verify where the data originated
	echo '<input type="hidden" name="bandsmeta_noncename" id="bandsmeta_noncename" value="' .
	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';

	// Get the soundcloud data if its already been entered
	$video = get_post_meta($post->ID, '_video', true);
        $soundcloud = get_post_meta($post->ID, '_soundcloud', true);
			$facebookLink = get_post_meta($post->ID, '_facebookLink', true);
	$festivalname = get_post_meta($post->ID, '_festivalname', true);
	$festivaldate = get_post_meta($post->ID, '_festivaldate', true);

	// Echo out the field
        echo '<p><span style="font-weight: bold; font-size: 18px; color: #F00">Video URL:</span></p>';
	 echo '<textarea id="texty" name="_video" class="widefat">' . esc_html( $video ) . '</textarea>';
        echo '<p><span style="font-weight: bold; font-size: 18px; color: #F00">SoundCloud Playlist URL</span></p>';
	 echo '<textarea id="texty" name="_soundcloud" class="widefat">' . esc_html( $soundcloud ) . '</textarea>';
		  echo '<p style="font-weight: bold; font-size: 18px; color: #F00">Facebook Link</p>';
	 echo '<input type="url" name="_facebookLink" value="' . $facebookLink  . '" class="widefat" />';

	   echo '<p style="font-weight: bold; font-size: 18px; color: #F00">Festival Appearances</p>';
	 echo '
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><p>Festival Name </p><br/><input type="text" name="_whichfestone" value="' . $festivalname  . '" class="widefat" /></td>
    <td><p>Festival Date </p><br/><input type="text" name="_whichfesttwo" value="' . $festivaldate  . '" class="widefat" /></td>
  </tr>
</table>
	';

	}

// Save the Metabox Data

function wpt_save_bands_meta($post_id, $post) {

	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times
	if ( !wp_verify_nonce( $_POST['bandsmeta_noncename'], plugin_basename(__FILE__) )) {
	return $post->ID;
	}

	// Is the user allowed to edit the post or page?
	if ( !current_user_can( 'edit_post', $post->ID ))
		return $post->ID;

	// OK, we're authenticated: we need to find and save the data
	// We'll put it into an array to make it easier to loop though.

	$bands_meta['_details'] = $_POST['_details'];
	$bands_meta['_video'] = $_POST['_video'];
    $bands_meta['_soundcloud'] = $_POST['_soundcloud'];
	$bands_meta['_festivalname'] = $_POST['_festivalname'];
	$bands_meta['_festivaldate'] = $_POST['_festivaldate'];

	// Add values of $events_meta as custom fields

	foreach ($bands_meta as $key => $value) { // Cycle through the $events_meta array!
		if( $post->post_type == 'revision' ) return; // Don't store custom data twice
		$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
		if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
			update_post_meta($post->ID, $key, $value);
		} else { // If the custom field doesn't have a value
			add_post_meta($post->ID, $key, $value);
		}
		if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
	}

}

add_action('save_post', 'wpt_save_bands_meta', 1, 2); // save the custom fields

// My template force code

function include_template_function( $template_path ) {
    if ( get_post_type() == 'bands' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'plugin-page.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/plugin-page.php';
            }
        }
    }
    return $template_path;
}

// My template force call
add_filter( 'template_include', 'include_template_function', 1 );
?>

wentzel on "How do I change the label for pingback comments?"

$
0
0

I like to get the automatic self-pingback comments. However, I do not like that they have the format \"Pingback: {Post title}\". I would like to customize this text, e.g. so that it says \"This post is mentioned in: {Post title}\". How can I do that?

I located the text \"Pingback: \" at two places in wp-includes/comment-template.php. But changing those texts doesn\'t seem to do any difference. I don\'t even understand to do this manually, as the comment text doesn\'t seem to be what shows up. When editing the comment it says something like \"[…] {part of post content} […]. However, this text doesn\'t show up at all on my post.

Any ideas?

c-m on "Continuous scroll"

$
0
0

I'm re-developing my wordpress website and like the idea of allowing my readers to continuously scroll through my posts, like is done all the news sites these days.

My site content lends itself well to this since it's a series of travel posts/stories. So for example I might have a category called Austria, then within that four or five posts about my trip there.

I'd like to this to run on the single.php or make use of ajax to show/expand the content when someone clicks on the "read more" link in a specific category archive.

The important part is that pagination still exists, and that the URL changes to show the post being viewed.

What's the best way of going about this? I prefer not to use plugins if possible.

Can anyone point me in the right direction?

Viewing all 8245 articles
Browse latest View live




Latest Images