Drupal url encoding - drupal

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 );

Related

Custom API Endpoint get data from category

im pretty stuck.. Im trying to get all post data from a specific category ID, using Wordpress REST API. But it dosen't output the content, only the ID & Title.. Cant see what I am doing wrong..
Heres the code:
/* Register Route http://dev.mpblogg.se/wp-json/api/v1/feedposts/id */
add_action( 'rest_api_init', function () {
register_rest_route( 'api/v1', '/feedposts/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'getAllPostsfromCategory',
));
});
/* Get all posts from the specific Caetgory */
function getAllPostsfromCategory( $data ) {
$secret = '2lpMh5EHaEiavhMONpWD';
$qs = explode('&', $_SERVER['QUERY_STRING'])[0];
$qs = explode('=', $qs)[1];
if($qs != $secret){
return false;
}
$posts = get_posts( array(
'category' => $data['id'],
));
$returnArray = array();
foreach($posts as $post) {
array_push($returnArray, array(
'id' => $post->ID,
'title' => $post->post_title,
'content' => $post->post_content
));
}
// die();
if(empty($posts)){
return null;
}
return $returnArray;
}
The JSON output looks like this:
After changing to array_push($returnArray,$post); it looks like this:
Okay... here's a clue that I got from the array_push image that you shared. The last string of the JSON output reads:
"filer": "raw"
This means that the resultant value would be stored as a raw, unfiltered content of the post.
So, you can try adding apply_filters() to the extracted content and see if it helps you display the required content.
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
This is used to apply the content filters to raw unfiltered post content, which usually comes from the use of $post->post_content .
As usual the answer why it didnt work is so dumb. Im not using the the_content.. Im using a custom field from ACF called post_excerpt. So I just added:
'excerpt' => get_field('post_excerpt', $post->ID),
Now it works.. Sorry all, but thank's for all the help.

Wordpress shortcode name as an Attribute

I was wondering if possible to setup a shortcode and have the name of the shortcode also work as an attribute. How I have mine currently setup is like so
add_shortcode('tooltip', 'tooltip');
function tooltip( $atts $content = null) {
array(
'type' => '',
);
So when someone in wordpress uses the shortcode you type in
[tooltip type="fruit"]Item Name[/tooltip]
Although I was wondering is it possible to just use the name of the shortcode as a atts so I can short it a little bit and have it look like this
[tooltip="fruit"]Item Name[/tooltip]
So pretty much cut out the type attribute and use the name of the shortcode tooltip as an attribute instead.
Nope, what you're proposing isn't possible. It may be shorter but in my opinion it would be confusing so I don't see it ever being something that's made possible short of you building the functionality yourself.
You have to use first item in $atts array, ($atts[0]) when using shortcode tag as attribute.
Working example:
<?php
add_shortcode('tooltip', 'tooltip');
function tooltip(Array $atts = array(), $content = null, $tag = null) {
$args = shortcode_atts(array( 0 => null ), $atts);
$args['type'] = trim($args[0], '"=');
unset($args[0]);
extract($args);
// Your code starts here ...
$output = array(
'$type' => $type,
'$content' => $content
);
$output = '<pre>' . print_r($output, true) . '</pre>';
return $output;
}
Please replace everything after // Your code starts here ...
Executing example:
[tooltip="fruit"]Item Name[/tooltip]
will return:
<pre>Array
(
[$type] => fruit
[$content] => Item Name
)
</pre>

redirection on MYMODULE_user_register_form_alter

ive been searching a lot and found a lot of post also but cant still make my code works. i dont really know the problem anymore, it is so hard to debug.
so i have this code module send_xml.module
i used the hook_form_FORMID_alter, it was like
function send_xml_form_user_register_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'send_xml_submit_function';
}
function send_xml_submit_function($form, &$form_state){
$email = $form_state['values']['mail'];
$password = $form_state['values']['pass'];
unset($_REQUEST['destination']);
unset($form['#redirect']);
$form_state['redirect'] = array(
'myurl',
array(
'query' => array(
'email' => $email,
'password' => $password,
),
),
);
}
i want this to redirect to an external link with the values from the regsitration form... but i can't get it to work... its my 3rd day now at is giving me a lot of pain really...
please help anyone please... it will be a big help. thank you very much! :)
In your submit method, give url instead of array using url() function to set redirect value. Currently you are giving array in $form_state['redirect]. Try following:
$form_state['redirect'] = url('myurl', array(
'query' => array (
'email' => $email,
'password' => $password)
)
);

Send URL with wp_remote_post not working

I am using a service that accepts URL in this format...
http://www.mydomain.com/zone1/code/verify/?data1=Apple&data1=Banana&data3=Orange&data4=Pear
I am trying to use wp_remote_post to submit it and receive a response....
$url = 'http://www.mydomain.com/zone1/code/verify/';
$fields = array();
$fields['data1'] = 'Apple';
$fields['data2'] = 'Banana';
$fields['data3'] = 'Orange';
$fields['data4'] = 'Pear';
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => $fields,
'cookies' => array()
)
);
This isn't working and the response I am recieving back is that the fields are missing.
Can anyone see anything wrong with my approach and also is there an easy way to echo out exactly the URL it is sending?
This url:
http://www..../verify/?data1=Apple&data1=Banana&data3=Orange&data4=Pear
is in a GET format, not POST so you should check the API docs for that request, but it likely does not support the POST format so you will need to use the GET syntax, like so:
$url = 'http://www.mydomain.com/zone1/code/verify/';
$url .= '?data1=Apple';
$url .= '&data2=Banana';
$url .= '&data3=Orange';
$url .= '&data4=Pear';
$args = array(); //the default arguments should be ok unless the API needs something special like authentication headers
$response = wp_remote_get($url, $args);
I like to use the kint-debugger plugin to dig through output like this, using that you could call dd($response); and get a nicely formatted display of the response. Without that plugin, just add this afterwards:
print '<pre>';
print_r($repsonse);
die();
This should show everything in the response variable.

Drupal 7 function to format an image URL?

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.

Resources