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

jubot on "How to define a custom field by using two functions?"

$
0
0

Hi.

I'm trying to automatically add a custom field to post. The custom field contains the url to the 'medium' thumbnail. I wan't to do this without defining a featured image and instead I want to use the first image from the post (image attachment). After an extensive search I've found the elements required to achieve this but I'm not able to customize it/make it work myself.

1. FUNCTION: Create custom field

The code used to create a custom field based on the featured image is the following:

function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $image = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), 'four' );
        return $image[0]; // thumbnail url
    } else {
        return 'http://domain.com/wp-content/uploads/2013/08/image.jpg';  // or a default thumbnail url
    }
}

add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
	global $wpdb;
	if(!wp_is_post_revision($post_id)) {
		add_post_meta($post_id, 'thumbnail_rt', w_thumbnail_src(), true);
	}
}

The best I can make of it in order to make it work with the post attached image is the following (wp_get_attachment_image_src):

function w_thumbnail_src() {
    if (wp_attachment_is_image()) {
        $image = wp_get_attachment_image_src( $attachment_id, 'four' );
        return $image[0]; // thumbnail url
    } else {
        return 'http://domain.com/wp-content/uploads/2013/08/image.jpg';  // or a default thumbnail url
    }
}

add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
	global $wpdb;
	if(!wp_is_post_revision($post_id)) {
		add_post_meta($post_id, 'thumbnail_rt', w_thumbnail_src(), true);
	}
}

As you can see wp_get_attachment_image_src requires an id ($attachment_id). This id is not associated with the post as is the case with a featured image id.

2. FUNCTION: Get $attachment_id

In order to find the $attachment_id the following code is used:

function pn_get_attachment_id_from_url( $attachment_url = '' ) {

	global $wpdb;
	$attachment_id = false;

	// If there is no url, return.
	if ( '' == $attachment_url )
		return;

	// Get the upload directory paths
	$upload_dir_paths = wp_upload_dir();

	// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
	if ( false !== strpos( $attachment_url, $upload_dir_paths['baseurl'] ) ) {

		// If this is the URL of an auto-generated thumbnail, get the URL of the original image
		$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url );

		// Remove the upload path base directory from the attachment URL
		$attachment_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $attachment_url );

		// Finally, run a custom database query to get the attachment ID from the modified attachment URL
		$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );

	}

	return $attachment_id;
}

3. How to combine these two functions?

How do I make one function out of these two functions, or how do i make it work. I want the output of the $attachment_id function to be used in the custom field function.

So to recap:

- how to make the custom field function work with image attachment instead of featured image/post_thumbnails?
- how to use the output from the $attachment_id function in the custom field function?

I've been dealing with this issue for a long time now and I feel like these bits of code are the solution. I just don't know how to put them together. Could use some help, thanks.


girll on "add_filter in wordpress oop plugin"

$
0
0

HI all. I am writing a wordpress plugin with OOP techniques. I need to add filter.I write it so:

add_filter('the_content', array($myObject, 'display_products'));

But in that case it starts to load page without end.
How should I write it right?
Thanks!

stitchmedia on "Set all publish dates to an offset value & have a movable TODAY?"

$
0
0

Here's my proposed hack:

We are setting up WP for a television series and we need events to happen on a FICTIONAL timeline - ie Character A posts two days after Ep 1 airs, Character B responds to Character A six hours later, etc.

WP is great for doing this in ABSOLUTE time (ie what day in reality did this post happen?) It's also great for displaying date/time relative to today (ie posted 8 days ago). What we need to do is to be able to shift time according to a broadcast schedule.

I would like to be able to enter each post with an 'offset' that will publish posts whenever a specific amount of time has elapsed from a 'start time' (ie the first episode broadcast). That way, when the show re-broadcasts six months later, it's an easy set-up to just change the 'start time' for the new broadcast and know that all the posts will go dormant until their offset time elapses before publishing.

Happy to clarify - not sure if this makes sense. Praying there's a plugin for this but we've got a coder to tinker too. Just looking for advice/ideas/tips.

vny009 on "Any plugin for register/login that uses database to store users"

$
0
0

Hello,

I am searching a registration/login plugin that uses databases to registered users.

Please share if anyone know!

freakable on "How to change this sql querry"

$
0
0
# First:

DELETE FROM wp_postmeta
WHERE post_id IN
(
SELECT id
FROM wp_posts
WHERE post_type = 'attachment'
)
;

# Second:

DELETE FROM wp_posts WHERE post_type = 'attachment'

That sql querry delete every attachment from post. I need to change this, to delete every attachment with height less than 100px. Can someone help me ?

chaos67731 on "Make multiple pages and set template/content on theme activation?"

$
0
0

OK, I have two different code and I need them mixed together more or less.

I want to be able to make 5 or 6 pages on a site when my theme is activated.

When it makes the pages, I want it to set the template that that page will use and set content to that page.

Here is code I have that will just make more than one page.

function create_initial_pages() {
    $pages = array(
        'page1' => 'Page 1',
        'page2' => 'Page 2',
        'page3' => 'Page 3',
        'page4' => 'Page 4'
    );
    foreach($pages as $page_url => $page_title) {
        $id = get_page_by_title($page_title);
        $page = array(
            'post_type'   => 'page',
            'post_name'   => $page_url,
            'post_title'  => $page_title,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_parent' => ''
        );
        if (!isset($id)) wp_insert_post($page);
    };
}

Here is code that will make only one page but set the content and template that page should have.

if (isset($_GET['activated']) && is_admin()){
        $new_page_title = 'Sitemap';
        $new_page_content = ' ';
        $new_page_template = 'sitemap.php'; //ex. template-custom.php. Leave blank if you don't want a custom page template.
        //don't change the code bellow, unless you know what you're doing
        $page_check = get_page_by_title($new_page_title);
        $new_page = array(
                'post_type' => 'page',
                'post_title' => $new_page_title,
                'post_content' => $new_page_content,
                'post_status' => 'publish',
                'post_author' => 1,
        );
        if(!isset($page_check->ID)){
                $new_page_id = wp_insert_post($new_page);
                if(!empty($new_page_template)){
                        update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
                }
        }
}

Thanks

maximumsavage on "Ajax Event Calendar - Remove PHP call"

$
0
0

Hello,

http://www.narwhalnation.com

I'm looking to remove the PHP call for the month and year in the AEC Widget:

<span class="fc-header-title"> <h2> August 2013</h2></span>

I'm assuming this is the way to go since I can't clear out the content using CSS overrides. I've looked in the plug-in editor but can't find where I would remove that.

I just think it looks clunky.

tunescool on "Move Button In Posting Page And Add Return Function"

$
0
0

i use this to add a button in my posting page

// add more buttons to the html editor
function wptit_add_quicktags() {
?>
    <script type="text/javascript">
    QTags.addButton( 'wptit_pre', 'Gallery', '<a href="http://url.com" target="_blank">See The Full Gallery Here</a>', '', 'g', 'See The Full Gallery tag', 1 );
    </script>
<?php
}
add_action( 'admin_print_footer_scripts', 'wptit_add_quicktags' );

someone helped me on another forum but didnt do everything i wanted

i want the button the last button, all the way on the rite, its the first button now

i want the button when hit to add the return key, as if the return has been hit and then this html

See The Full Gallery Here

if i have the cursor after the g's, highlite the rest, and hit the button

gggggggggggggggghhhhhhhhhhhhhhhhh

this is what the button does

gggggggggggggggg
See The Full Gallery Here

return and it goes to a new line and the html is added


beth1l1 on "How do you allow users to change the displayed time based on their timezone?"

$
0
0

How do you allow the timezone displayed to be changed by the user or automatically based on the users timezone?

adamshriki on "popular posts this month not all time!"

$
0
0

Hi!
We have this site:
hafla
on the left side is the popular posts by this code:

$side_posts = new WP_query();
                                    $side_posts->query(array('category__in'=>array(16), 'posts_per_page'=>3, 'post_type'=>'post','meta_key'=>'_hafla_pageview','meta_value'=>0, 'meta_compare'=>'>', 'orderby'=>'meta_value_num', 'order'=>'DESC'));
                                    while($side_posts->have_posts()){
                                    $side_posts->the_post();

but it's the popular posts by all times, and I want popular posts this month.

how can i achieve this?

thanks in advance,

Adam Shriki.

Esikad on "Accessing full post for split content"

$
0
0

Dear All,

I have written a plugin that builds a table of content from the data in my blog posts using content filters. Unfortunately, it does not play well with posts that are split in pages (using <!--nextpage-->) because the filter does not see the full content of the post, but only the page being currently served. As a result, I get a different (crippled) table of content on each page of the article, matching only the displayed content.

How can my filter access the full content of the post (I guess WordPress has to load it in full?). If that's not possible, how can I load it from the database from my plugin ?

Thanks for your help.

A31 on "Insert PDF File to Custom Database Table"

$
0
0

Hi all,

Can anyone please advise me how I can insert a PDF file that a user selects from a html form into a custom table in the database?

My code so far is:
$OrderNumber= $_POST['txt_OrderNumber'];
$UploadAttachment = $_POST['txt_Attachment'];

$wpdb->insert('wp_Attachments',
array(
'OrderNumber' => $NextOrderNumber,
'FileContent' => $UploadAttachment
),
array(
'%s',
'%s'
)
);

Although I do not get any errors, the size returned in the database is 0.

Any advice please?

vmandesign on "WP_Query and paging problem"

$
0
0

So I've got this code placed on both my index.php page and my single.php page.

The idea is to show the most recent four posts, excluding the current post being displayed. I'd also like the next_posts_link and previous_posts_link to display the appropriate pages of results.

The issue is that the code works perfectly on my index.php page, but on the single.php page, when you click on the next/prev page links it re-shows the same four posts.

Please, can someone help me find out why the code is being treated differently?

<?php
global $paged;
$this_post = $post->ID;
$wp_query = new WP_Query();
$wp_query->query(array('post__not_in' => array($this_post), 'posts_per_page' => 4, 'paged' => $paged)); ?>

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div style="width: 224px; float: left; margin-right: 10px;">
<a href="<?php the_permalink() ?>"><h4><?php the_title(); ?></h4></a><br />
<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(224,224)); ?></a>
</div>
<?php endwhile; ?>
<div style="clear: both;"></div>
<?php next_posts_link('&laquo; Older Entries', $wp_query->max_num_pages) ?>
<?php previous_posts_link('Newer Entries &raquo;') ?>
<?php wp_reset_query(); ?>

1234567890-asdfghjklzxcvbnm on "Dynamic Link shows on all pages, only want last four."

$
0
0

Hey everyone, posted this (for the second time) on the plugin thread. No responses. Hopefully you guys can be of some help.
I'm working on a Genesis slider, and have 5 slides. One of those is supposed to be just a landing slide- everytime you visit the page it will always be the first one and has 4 summery points. The other 4 slides give an in depth perspective into each individual point. I have a php code (shown below) that shows a link to the page on every slide. Great. Except the first slide doesn't have nor needs any content and it's link needs to be removed. I've tried comparing page IDs (pretty sure I'm doing page IDs) to the page ID '1211'. To no avail, if page_id == 1211 -> all of the links appear. If page_id != 1211-> None of the links appear. And by 'page_id' I mean the actual variable/ array named 'page_id' and also a couple of functions that are meant to obtain a page ID.
If you could help me out, that would be great. Thanks.
<a href="<?php the_permalink() ?>" rel="bookmark">Click here to learn more

And also
'<?php if ($id==1); { ?>
" rel="bookmark">Click here to learn more
<?php } ?>'

Note,the_ID() and get_permalink( $id) are a couple I've tried. But no dice. And it doesn't matter what I use in the IF statement. All it really cares about is the operator.

benvolio on "Wordpress Adminbar - wp-admin-bar-top-secondary"

$
0
0

I was just wondering how i could put the wp-admin-bar-top-secondary (account section of wp-adminbar) in my site header via header.php; is there any kind of php function to summon this in my header.php file; could who ever knows how to do this kindly assist me with this tiny issue. Thanks in advance :)

*oh, and am using a local server to test my site.


m0ngr31 on "update_post_meta() adding extra things"

$
0
0

I'm trying to use the update_post_meta function to update a post when a button is pushed so that it will change something in the meta data for the post.

This is how I'm calling it:
update_post_meta($post_id, '_myrp_rating_categories', 'a:4:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";}' );

But for some reason, the data ends up looking like this:
s:54:"a:4:{i:0;s:1:"1";i:1;s:1:"2";i:2;s:1:"3";i:3;s:1:"4";}";

Is there something I'm missing here?

NVZ on "username_exists does not work with special chars"

$
0
0

Hi there,

I may have stumbled upon a bug in the wordpress system. I have a script were i need to check the username before adding it with wp_insert_user.

In wp_users i have a user with a user_login value of: Cafe t test

When i try to insert the following username in wp_insert_user the wp_error tells me that the username exists already: Café 't test
That seems to be correct, so i need to check the value before adding with username_exists(). But that function returns null.. ?
When i remove the accents and the quotes both functions tell me the username exists. But in my opinion, the username_exists functionn should do that by default.

ayruncom on "about hacking my web site (ayrun.com)"

$
0
0

I am using a plugin wordfence which is excellent. Few days ago it warns me twice that my HTML, PHP,files ,core etc. has been severely changed; and they fixed eveything again. But, now when I check wordfence I saw that I have 2 themes which is not correct. I am using ofonly Twenty-eleven theme. But, according to wordfence, I have also twenty-thirteen themes too. Who put this theme in my site? I don't know. Besides that, today I visitmy account in Wordpress. com . Surprisingly I saw that I have 35 pages (articles). İn reality I have only 33 articles. Two more articles comes from where? I don'T know. How can I stop hackers to reach my website? Do I have opprtunity to find them? I will delete all of my articles from this website; and keep my website inactive. May be in that way I can prevent their actions. Do you think it works? Please, help me to solve this problems. İt makes me fed-up.
Thank you.
Yours sincerely,
Ayşe Nuray Ünyay

roylodder on "Custom field array"

$
0
0

Hi There!

I’m working on an existing WordPress website. The site uses custom fields and one field is populated with an array.
The custom field is called “items” and I need the value of the “description” key.

Custom field name: items
Array key I need the value of: description
Array: `a:33:{
s:4:”name”;
s:8:”janedoe”;
s:4:”language”;
s:18:”English, Dutch”;
s:12:”hobbies”;
s:0:”";
s:7:”work”;
s:0:”";
s:12:”description”;
s:48:”Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aperiam, soluta!”;
}`

Is there an easy way to do this?

Best Roy

joshdd on "Template for child archive of custom post type"

$
0
0

Hello all :)

So I have a custom post type used by a plugin - lets call it CAKE.
In the archives, there are several sub sections of CAKE - lets call them CUSTARD, CREAM and SAUCE.
And CUSTARD has 2 sub categorys - lets call them HOT and COLD

Now when viewing CAKE archive at http://www.mywebsite.co.uk/cake it uses my custom template file archive-cake.php but when you view any of the sub sections of it, for example CUSTARD at http://www.mywebsite.co.uk/cake/custard it just uses the standard archive template (archive.php) - how can I get them to use my custom template?

ADDITIONALLY - in my header.php I'm using if ( is_post_type_archive('cake') ):
to add a something to the header for cake archive pages, but again the sub-pages of cake such as custard aren't responding to this.

I'm working local so can't provide real links - sorry.

In summary:
http://www.mywebsite.co.uk/cake uses archive-cake.php but http://www.mywebsite.co.uk/cake/custard uses archive.php, so how do i get http://www.mywebsite.co.uk/cake/custard to use archive-cake.php

And how do I get http://www.mywebsite.co.uk/cake/custard to respond to is_post_type_archive('cake')

Thanks,
Josh.

Viewing all 8245 articles
Browse latest View live




Latest Images