How to Add Wordpress Filter to add_filter to next_post_link - wordpress-theming

How can I add the PARAMETERS to this WP function next_post_link().
add_filter ('next_post_link', 'new_next_post_link');
function new_next_post_link ($args) {
in_same_cat ??????
return $args;
}
I need to change it via FILTER cause I cant change it in the Theme. I tried this.
add_filter ('next_post_link', 'new_next_post_link');
function new_next_post_link ($args) {
$args = "Hello World";
return $args;
}
This works, so I need to know how to change "Hello World" into next_post_link('%link', 'Next post in category', TRUE);
Cheers,
Denis

you don't need to. the third parameter is in_same_cat (bool). by default it is set as false.
<?php next_post_link($format, $link, true); ?>
http://codex.wordpress.org/Function_Reference/next_post_link

Related

ACF overrides HTML attributes added with load_value hook

I'm using ACF together with the ACF Hook acf/load_value to add a custom HTML wrapper to the ACF value. I use then the ACFs to build an Elementor template (I'm using Elementor PRO).
The Template works and the values of ACFs are rendered, but the attribute I've added in the wrapper disappear
I've tried to change the priority of my filters, but it wasn't the problem. I 've also tried to look into the ACF settings, but seems that I cannot change this behavior just changing some settings.
This is the filter I made
if (!function_exists('my_acf_span_property')) {
function my_acf_span_property($value, $property) {
$value = '<span property="' . $property . '">' . $value . '</span>';
return $value;
}
}
if (!function_exists('my_acf_industry_value')) {
function my_acf_industry_value($value)
{
return my_acf_span_property($value, 'industry');
}
}
add_filter('acf/format_value/name=industry', 'my_acf_industry_value');
I made one filter for each ACF I need to change, this is only one as example.
I've tried to debug the filter changing return $value; to return htmlentities($value); in the function my_acf_span_property and the attributes are rendered in the frontend.
The output was expected to be <span property="industry">ACF value</span>
But wat is rendered is <span>ACF value</span>
It could be an Elementor problem?
Any Idea?
I solved with an action to allow the attributes in the posts
if (!function_exists('allow_property_attribute')) {
function allow_property_attribute() {
global $allowedposttags, $allowedtags;
$newattribute = "property";
$allowedposttags["span"][$newattribute] = true;
$allowedtags["span"][$newattribute] = true;
$allowedposttags["div"][$newattribute] = true;
$allowedtags["div"][$newattribute] = true;
$allowedposttags["p"][$newattribute] = true;
$allowedtags["p"][$newattribute] = true;
}
}
add_action( 'init', 'allow_property_attribute' );
It looks like was WordPress my problem, and not Elementor or ACF.

Add default code in wordpress post

I want to add a simple code to all new post by default
I tried to used this code in .function.php (wpbeginner)
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "If you like this post, then please consider retweeting it or sharing it on Facebook.";
return $content;
}
I am using plugin named Shortcodes Ultimate
Code:
[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]
I tried to normal use simple html and CSS code but it show the same error for both cases
https://i.stack.imgur.com/wgAs4.png
Try this -: wrap your code is a single quote as you are already using double quotes.
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = '[su_spoiler title="Download The File" style="fancy" icon="chevron-circle"]Here[/su_spoiler]';
return $content;
}

Yii 2 use same controller method / action in multiple controllers

I have a method checking whether user is logged in in my UserController.I need the same method in all the other controllers.How can I do the same without copy pasting the code to all controllers.
The controller method looks like
public function is_logged_in() {
$session = Yii::$app->session;
$cookies = Yii::$app->request->cookies;
//print_r($session);
$session->open();
$session_cookie_name = Yii::$app->params['cookie_name_session_var'];
$logged_in = false;
//echo "-memn-".$cook_name.' is halle - ';
//print_r($_SESSION);
if(($cook_name = $session->get($session_cookie_name))) {
//echo " - <pre>";
//print_r($cookies);
//exit;
$write_cookies = Yii::$app->response->cookies;
//echo "</pre>";
//echo $cookies->getValue($cook_name).' placenta';
if($u_token = $cookies->getValue($cook_name)) {
echo "b";
if($u_token) {
echo "c";
$write_cookies->remove($cook_name);
unset($write_cookies[$cook_name]);
$session->destroy();
$session->open();
$cookie_name = sha1($u_token).time();
$session[$session_cookie_name] = $cookie_name;
$write_cookies->add(new \yii\web\Cookie([
'name' => $session[$session_cookie_name],
'value' => $u_token,
'expire' => time() + 6000000
])); // around one hour expiry time
$session->close();
$logged_in = true;
//echo $u_token;
}
}
}
if(!$logged_in) {
$session->destroy();
}
return $logged_in;
}
1) You can create own component and put this method here or place it in the model (depends on logic of that method). Component can be placed for example in components folder (by default it doesn't exist). Then just use this component in any controllers you want.
2) If this code needs to be executed before or after certain actions, you can use behaviors.
3) You can use inheritance and create your custom controller that extends from yii\web\Controller, declare this method here and extend all other controllers where are you going to use this logic from your custom one.
In addition to arogachev's answer your code actually should sit in a class that extends the User component class http://www.yiiframework.com/doc-2.0/yii-web-user.html , not even to mention that the user identity class already does everything your code does (only much, much better). It comes with isGuest function.

MediaWiki Extension question / suggestion

Complete beginner here. I want to create a new tab on each page that has a custom action. When clicked, it takes you to a new page which has custom HTML on it along with the text or the original article.
So far I could create a new Tab and could give a custom action mycustomaction to it. I am pasting what I did so far here. Please let me know if I am using the correct hooks etc. and what is a better way to achieve this basic functionality.
So far with their docs I have done this:
#Hook for Tab
$wgHooks['SkinTemplateContentActions'][] = 'myTab';
#Callback
function myTab( $content_actions) {
global $wgTitle;
$content_actions['0'] = array(
'text' => 'my custom label',
'href' => $wgTitle->getFullURL( 'action=mycustomaction' ),
);
return true;
}
#new action hook
$wgHooks['UnknownAction'][] = 'mycustomaction';
#callback
function mycustomaction($action, $article) {
echo $action;
return true;
}
This gives me error:
No such action
The action specified by the URL is invalid. You might have mistyped the URL, or followed an incorrect link. This might also indicate a bug in the software used by yourplugin
What I was doing wrong:
$content_actions[‘0’] should simply be $content_actions[] (minor nitpick)
$content_actions is passed-by-reference, it should be function myTab( &$content_actions ) {}
mycustomaction() should do something along the lines of
if ( $action == ‘mycustomaction’ ) {
do stuff; return false;
}
else {
return true;
}
It should use $wgOut->addHTML() instead of echo
Thanks a lot everyone for your help!

CakePHP - get last query run

I want to get the last query CakePHP ran. I can't turn debug on in core.php and I can't run the code locally. I need a way to get the last sql query and log it to the error log without effecting the live site. This query is failing but is being run.
something like this would be great:
$this->log($this->ModelName->lastQuery);
Thanks in advance.
For Cake 2.0, the query log is protected so this will work
function getLastQuery() {
$dbo = $this->getDatasource();
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
Tested in CakePHP v2.3.2
$log = $this->Model->getDataSource()->getLog(false, false);
debug($log);
In CakePHP 1.x, the data you want is accessible in DataSource::_queriesLog. Cake doesn't really provide a getter method for this member, but the underlying language being PHP, nothing stops you from doing the following:
In app/app_model.php:
function getLastQuery()
{
$dbo = $this->getDatasource();
$logs = $dbo->_queriesLog;
return end($logs);
}
You can use this inline.
$dbo = $this->Model->getDatasource();
// store old state
$oldStateFullDebug = $dbo->fullDebug;
// turn fullDebug on
$dbo->fullDebug = true;
// Your code here! eg.
$this->Model->find('all');
// write to logfile
// use print_r with second argument to return a dump of the array
Debugger::log(print_r($dbo->_queriesLog, true));
// restore fullDebug
$dbo->fullDebug = $oldStateFullDebug;
Simple you can use showLog() function
var_dump($this->YourModel->getDataSource()->showLog());
This is a very late answer, i know, but for whoever needs this in the future, you can always restrict setting debug to your IP, For example:
Configure::write('debug', 0);
if($_SERVER["REMOTE_ADDR"] == '192.168.0.100'){
Configure::write('debug', 2); //Enables debugging only for your IP.
}
Combination of Matt's and blavia's solution (works when debug is not 2):
$dbo = $this->Model->getDatasource();
$oldStateFullDebug = $dbo->fullDebug;
$dbo->fullDebug = true;
// find or whatever...
$this->Model->find("all");
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
CakeLog::write("DBLog", $lastLog['query']);
$dbo->fullDebug = $oldStateFullDebug;
Having a quick skim of the book, cakephp api getLog you could turn on logTransaction. Although having not used it, I'm not sure how it will perform.
Otherwise you could experiment with FirePHP and here is the a guide for it,
You might try DebugKit, although off the top of my head I think you do still need debug 2 to get it to work.
Hopefully something might give you a lead. :)
You can use this:
$log = $this->Model->getDataSource()->getLog(false, false);
pr($log);die;
There are two methods to view the query in CakePHP.
Both methods you have to add the below one line in app/Config/core.php
Configure::write('debug', 2);
First methods :
debug($this->getLastQuery());
where you want to get the query add above line and call this function getLastQuery() on same controller using below code
public function getLastQuery() {
$dbo = $this->TModel->getDatasource(); //Here TModel is a model.what table you want to print
$logs = $dbo->getLog();
$lastLog = end($logs['log']);
return $lastLog['query'];
}
second method :
add the below line in the any Elements files.
<?php echo $this->element('sql_dump'); ?>
This will help you.
echo '<pre>';
$log = $this->YOUR_MODEL->getDataSource();
print_r($log);
exit;