Drupal 7 function to format an image URL? - drupal

I am creating a module in Drupal 7. If I want to format a UNIX timestamp into a date I can use the format_date function, like so:
$date = format_date($node->created, 'custom', 'j F Y');
Is there also a function I can reference to format an image? Say my database query returns
picture-4-136576449.png
from a table and I want to transform it into:
<img src="/images/picture-4-136576449.png />
is there a function to do it? Thanks.

If you are trying to convert a URI to a URL, you are doing it wrong.
$uri = 'public://images/my-photo.png';
$url = file_create_url($uri);
// $url should be publicly accessible URL now.
If you know the path to the image relative to Drupal root, use
$url = url('path/to/image.png', array('alias' => FALSE));
You can theme an image using theme('image', $variables).
ex:
<?php
print theme('image', array('path' => 'path/to/image.png',
'title' => t('Some title'), //optional
'alt' => t('Some alt text'), //optional
));
You can see the other variables at theme_image() function doc page.

Related

Problem with update_field of ACF img associated with User

I have an edit profile page with a form POST, in which there's an input file, that upload image via ajax on the server and in the media library and return the filename in a textarea.
I can't update field with the url of the image that I uploaded via ajax. The Image appears in the media library and in server folder, but my acf field doesn't update.
The field is set to return img link.
This is my code:
$wp_upload_dir = wp_upload_dir();
$filename_path = $wp_upload_dir['path'] . "/$img"; //img is the value of POST of the textarea and return the filename "ARCW8764.JPG"
$filename_url = $wp_upload_dir['url'] ."/$img";
$wp_filetype = wp_check_filetype(basename($filename_path), null );
$attachment = array(
'guid'=> $wp_upload_dir['url'] . '/' . basename( $filename_path ),
'post_mime_type' => $wp_filetype['type'],
'post_title' => $img,
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment);
update_field('foto-agente',$filename_url,$user);
What's wrong?
Given that everything else is correct. Probably you are not targeting the field correctly. You need to go to the database in the table that is stored. I am guessing either wp_postmeta or wp_usermeta and check the name of your field. It is probably something like something_foto-agente. This is how ACF is storing nested fields, ex: parent_child_grandchild_name-of-field.
/* if the field has no record, you will not find it. Ensure to store something from the admin first, then see the name of the field in DB and then try it from your code. Let me know, good luck!

How to show the title of the embedded audio file in WordPress

audio_list(get_the_content())
function audio_list($data){
$content = strip_tags($data);
$audiolist = explode("[/audio]", $content);
$audiolist = array_filter($audiolist);
return $audiolist;
}
Right now am using a custom function that strip the audio url and split the file name from the url and showing the real file name as the name of the audio in my site. I need to change the real name as media title. How can i do this ?? In word press embedded player media id is not available. In bulk upload case media is not associated with any posts so parent post attachment concept also not working. Do have any other method to get the media title ??
Get the uploaded audio files as an array first
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'audio',
'numberposts' => -1
);
$audiofiles = get_posts($args);
create a if condition that matching the file url's
foreach ($audiofiles as $file)
{
$urls = wp_get_attachment_url($file->ID);
if($urls == 'url from the content'){
$title = $file->post_title;
}else {
$title = 'file real name';
}
}

Buddypress bp_activity_add(activity_action) hides the link "target"

I'm developing a social network with Buddypress, I created a RSS plugin to pull the RSS feed from the specified websites.
Everything is working, except when the RSS is posted to the activity stream. When I create the activity content to print a link, I set the link target to "_new" to open it in a new page.
Here's the code:
function wprss_add_to_activity_feed($item, $inserted_ID) {
$permalink = $item->get_permalink();
$title = $item->get_title();
$admin = get_user_by('login', 'admin');
# Generates the link
$activity_action = sprintf( __( '%s published a new RSS link: %s - ', 'buddypress'), bp_core_get_userlink( $admin->ID ), '' . attribute_escape( wprss_limit_rss_title_chars($title) ) . '');
/* Record this in activity streams */
bp_activity_add( array(
'user_id' => $admin->ID,
'item_id' => $inserted_ID,
'action' => $activity_action,
'component' => 'rss',
'primary_link' => $permalink,
'type' => 'activity_update',
'hide_sitewide' => false
));
}
It should come up with something like that:
Test
But it prints like that:
Test
Why is this happening?
The 'target' attribute is probably getting stripped by BuddyPress's implementation of the kses filters. You can whitelist the attribute as follows:
function se16329156_whitelist_target_in_activity_action( $allowedtags ) {
$allowedtags['a']['target'] = array();
return $allowedtags;
}
add_filter( 'bp_activity_allowed_tags', 'se16329156_whitelist_target_in_activity_action' );
This probably won't retroactively fix the issue for existing activity items - it's likely that they had the offending attribute stripped before being stored in the database. But it should help for future items.

Upload an image to Wordpress from URL

I'm creating a user-submitted post tool for a Wordpress site.
The user can enter an image URL and I want it to be added to the post along with other content like title and post content.
I've got this working using a regular image attachment. I create the post and then attach the image using this script:
$files = $_FILES['upload_attachment'];
$file = array(
'name' => $files['name'],
'type' => $files['type'],
'tmp_name' => $files['tmp_name'],
'error' => $files['error'],
'size' => $files['size']
);
$_FILES = array("upload_attachment" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$userPost);
}
But how can I do the same thing using insert_attachment() or similar but with a URL?
I think you can use file_get_contents() and file_put_contents(). You'd probably have to do something like this:
file_put_contents('your_file_on_server.xxx', file_get_contents('http://someurl.com/filename.xxx'));
This would download the file from the specified url and save it to your server. From there you can slightly modify your original code to attach it to a post.

Drupal url encoding

I am having trouble properly encoding URL data. Using the following code:
$redirect = drupal_urlencode("user/register?destination=/node/1");
drupal_goto( $redirect );
but, the URL that comes up in my browser test is the following:
http://testsite.com/user/register%253Fdestination%253D/node/1
I thought using the drupal_urlencode function should fix this encoding issue.
Can anyone suggest a way to fix this, please?
You'd be better off using the built in url() function to create your URL, if you pass an array as the query parameter it handles URL encoding for you:
$options = array(
'absolute' => TRUE,
'query' => array('destination' => '/node/1')
);
$redirect = url('user/register', $options);
drupal_goto( $redirect );
drupal_encode() will encode the whole string that you pass to it, so if you want to do it your original way it would look like this:
$redirect = 'user/register?' . drupal_urlencode("destination=/node/1");
drupal_goto( $redirect );
The simplest way of doing this in Drupal 6 is:
drupal_goto("user/register","destination=/node/1");
The below code from Clive worked for me..
$options = array(
'absolute' => TRUE,
'query' => array('destination' => '/node/1')
);
$redirect = url('user/register', $options);
drupal_goto( $redirect );

Resources