07-27-2023, 09:24 AM
I'm trying to add a URL GET parameter to one of my main menu items in Wordpress(but I don't know how to). So, my approach was to detect a click event on the menu item, then pass a parameter via AJAX to my php page which will process value passed as needed.
My main questions are, looking at my code, how come is not working? is there a better way of doing this in WordPress and not rely on javascript?
Here is the javascript:
```
<script type="text/javascript">
$(document).ready(function() {
$("#menu-item-128").click(function() {
$.ajax({
url: 'homepage.php',
type: "GET",
data: ({ homeclick = true }),
success: function() {
alert("success!");
}
});
});
});
</script>
```
Here is my PHP:
```php
$homeclick = $_GET['homeclick'];
if ( !isset( $_COOKIE['hs_user'] ) ) {
get_header();
} elseif (isset( $_COOKIE['hs_user'] ) && $homeclick == true ) {
get_header();
} else {
// Do Something else
header('Location: homepage-returning-users');
}
```
My main questions are, looking at my code, how come is not working? is there a better way of doing this in WordPress and not rely on javascript?
Here is the javascript:
```
<script type="text/javascript">
$(document).ready(function() {
$("#menu-item-128").click(function() {
$.ajax({
url: 'homepage.php',
type: "GET",
data: ({ homeclick = true }),
success: function() {
alert("success!");
}
});
});
});
</script>
```
Here is my PHP:
```php
$homeclick = $_GET['homeclick'];
if ( !isset( $_COOKIE['hs_user'] ) ) {
get_header();
} elseif (isset( $_COOKIE['hs_user'] ) && $homeclick == true ) {
get_header();
} else {
// Do Something else
header('Location: homepage-returning-users');
}
```