Support Forum
Support › General › Using Custom url field in Custom Post Type for redirect with Javascript / JqueryResolved
Hi,
Would like to be able to dynamically redirect my custom post to external url link that is in the custom url field in that post.
I am using the following:
jQuery(document).ready(function($) {
$(window).attr('location', "<?php echo rwmb_meta( 'extrnl_url' )>;");
$(location).attr('href', "<?php echo rwmb_meta( 'extrnl_url' ) ?>;");
});
However this is not working. if I replace this <?php echo ?> with a fixed url, this script works. any help on this is greatly appreciated
Hi,
It looks like you are using the PHP code in the JavaScript file (.js). Please refer to this topic to know how to write PHP and JS code in WordPress.
https://stackoverflow.com/questions/47117329/adding-javascript-to-php-wordpress
Hi Long
Thanks for your reply,
What is my actual problem, is retrieving the url value from custom url field into the script?
I was following the <a href= <?php echo .. example from one of the topic in this forum.
Is there anyway to retrieve this dynamic extrn_url custom field into the javascript so that the redirect will work.
I tried using MB view shortcode and $extrn_url in php, I can't seem to get the url value into the script.
Appreciate your help on this.
Best regards
Just so you can understand more,
Field ID: extrn_url
I tried the following in PHP and nothing is being retrieve.
<?php
echo rwmb_get_value($extrn_url);
?>
and
<?php
echo rwmb_get_value('extrn_url');
?>
and I also tried the following code:
<?php
$value = rwmb_get_value ('extrn_url' , $_POST['post_id']);
echo $value;
?>
also tried with rwmb_meta
<?php
echo rwmb_meta('extrn_url');
?>
Still no results.
Tested directly from Oxygen custom field metabox, it works, but not with the PHP. or for that matter I tried it in javascript, it also did not work. I tried passing the Var from Java script to PHP, still not working.
[oxygen ct_sign_sha256='43a6a06eaad99a3ea156b2b8b4538c3fc993920929650b8dc077274a5437a2b2' data='custom_metabox_content' settings_path='affiliate_url' settings_page='' ]
Any help on this is greatly appreciated. Not sure where I go wrong.
Hi Leo,
Where did you add the PHP code to show the field value?
<?php
echo rwmb_meta('extrn_url');
?>
You should add it to the post template of the theme to show field value on the single post.
Refer to this documentation https://docs.metabox.io/displaying-fields/#using-code
Hi Long
I added this php code in oxygen template page using code block.
Hi,
In case of using Oxygen Builder, please reach to them if you have any issues with installation, configuration, compatibility, or usage.
Refer to our support policy https://support.metabox.io/topic/support-policy/