WordPress本身并不直接提供在Cookie中记录用户搜索历史的功能。但是,你可以通过自定义代码来实现这样的功能。以下是一种可能的实现方法:
function setSearchCookie() {
var searchTerm = document.getElementById('searchinput').value; // Assume search input has ID 'searchinput'
document.cookie = 'search_history=' searchTerm '; path=/';
}
<form id="searchform" action="#" onsubmit="setSearchCookie()">
<input type="text" id="searchinput" placeholder="Search" />
<button type="submit">Search</button>
</form>
function getSearchHistory() {
var cookies = document.cookie.split('; ');
var history = '';
for (var i = 0; i < cookies.length; i) {
var cookie = cookies[i].split('=');
if (cookie[0] === 'search_history') {
history = decodeURIComponent(cookie[1]);
}
}
return history;
}
// Call this function wherever you want to display the search history
var searchHistory = getSearchHistory();
if (searchHistory !== '') {
console.log('Search history: ' searchHistory);
}
请注意,这只是一个简单的示例,并且可能需要根据你的特定需求进行调整和扩展。还应该考虑用户隐私和合适的法律规定,确保你的实现符合适用的隐私政策和法律法规。