Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 665 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wordpress: How can I add url GET parameter to my main menu items

#1
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');
}
```
Reply

#2
I would suggest to use custom menus in Appearance>Menus. It will help you to retain custom URLs with get parameters. You may read it here [Wordpress Menu][1]


[1]:

[To see links please register here]

Reply

#3
The problem is that you are attempting to pass a boolean value in your Ajax call with the variable `homeclick`. GET requests simply use text because the data is passed thru the URL, so if you are wanting a logical/boolean type you can use either "true" and "false" in text or perhaps 0 and 1. Also you have a syntax error, see below.

Try the following:

In you ajax call, fix the syntax as well as set homeclick to "true" as follows:
`data: ({ homeclick: 'true' }),`.

And in your php, change the if condition for variable `$homeclick` as follows:
`$homeclick == 'true'`.

You may want to consider using a POST method if you want to utilize a boolean.
Reply

#4
The filter hook `wp_get_nav_menu_items` is used to manipulate the Nav Menus. The `post_title` used in the example is the title of the Menu (Navigation Label), not of the post/page.

![home nav menu][1]

Drop this code in your `functions.php` file, adjust the `post_title` and `?my_var=test` to your needs. Note that better than `functions` is to [create your own plugin][2].

add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );

function nav_items( $items, $menu, $args )
{
if( is_admin() )
return $items;

foreach( $items as $item )
{
if( 'Home' == $item->post_title)
$item->url .= '?my_var=test';

}
return $items;
}


[1]:

[2]:

[To see links please register here]

Reply

#5
That's a fork from Brasofilo that works perfectly for me:
(Put this code on your theme functions.php)

// Transform title attributes to parameters
add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args )
{
if( is_admin() )
return $items;
foreach( $items as $item )
{
if ($item->attr_title != "") $item->url .= '#' . $item->attr_title;
}
return $items;
}

What I'm doing here is to get the attributes of the menu-item and transform them to an anchor name. This way I will have this type of URL's:

[To see links please register here]


After this I just need to do some jQuery magic to jump (progressive scroll down) to the anchor. ([Codepen][1]).

If you cannot see the 'Title Attribute' input field just be sure to check the options on the top "Screen Options" button of Wordpress Admin menu...


[1]:
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through