Using Custom url field in Custom Post Type for redirect with Javascript / Jquery
Support › General › Using Custom url field in Custom Post Type for redirect with Javascript / JqueryResolved
- This topic has 6 replies, 2 voices, and was last updated 3 years, 3 months ago by
Long Nguyen.
-
AuthorPosts
-
December 30, 2021 at 6:17 PM #32930
Leo Hermanto
ParticipantHi,
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
December 31, 2021 at 12:34 PM #32945Long Nguyen
ModeratorHi,
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-wordpressDecember 31, 2021 at 5:36 PM #32948Leo Hermanto
ParticipantHi 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
January 1, 2022 at 4:35 PM #32955Leo Hermanto
ParticipantJust 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.
January 1, 2022 at 10:39 PM #32957Long Nguyen
ModeratorHi 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-codeJanuary 2, 2022 at 4:57 AM #32958Leo Hermanto
ParticipantHi Long
I added this php code in oxygen template page using code block.
January 2, 2022 at 4:51 PM #32960Long Nguyen
ModeratorHi,
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/ -
AuthorPosts
- You must be logged in to reply to this topic.