阅读视图

发现新文章,点击刷新页面。

state 和 props 之间的区别是什么?

state 和 props 之间的区别是什么?

props(“properties” 的缩写)和 state 都是普通的 JavaScript 对象。它们都是用来保存信息的,这些信息可以控制组件的渲染输出,而它们的一个重要的不同点就是:props 是传递组件的(类似于函数的形参),而 state 是在组件被组件自己管理的(类似于在一个函数内声明的变量)。

下面是一些不错的资源,可以用来进一步了解使用 props 或 state 的最佳时机:

测试一下WordPress后台SVG格式图片上传

试着用纯代码的方式来开启wordpress SVG格式图片上传功能。目前看使用Block编辑器图片能正常上传和显示。

function minuo_allow_additional_mime_types($mime_types) {
	if ( ! current_user_can( 'administrator' ) ) {
		return $mime_types;
	}
	$mime_types['svg'] = 'image/svg+xml';
	$mime_types['svgz'] = 'image/svg+xml';
	$mime_types['webp'] = 'image/webp';
	$mime_types['ico'] = 'image/vnd.microsoft.icon';
	return $mime_types;
}
add_filter('upload_mimes', 'minuo_allow_additional_mime_types');

function minuo_wp_check_filetype_and_ext( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
	if ( ! $wp_check_filetype_and_ext['type'] ) {
		$check_filetype  = wp_check_filetype( $filename, $mimes );
		$ext = $check_filetype['ext'];
		$type = $check_filetype['type'];
		$proper_filename = $filename;
		if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
			$ext  = false;
			$type = false;
		}
		$wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
	}
	return $wp_check_filetype_and_ext;
}
add_filter('wp_check_filetype_and_ext', 'minuo_wp_check_filetype_and_ext' , 10, 5);

strlen、mb_strlen、str_word_count的区别

strlen() 、mb_strlen()和 str_word_count() 是 PHP 中的三个字符串处理函数。

strlen — 获取字符串长度。

strlen() 函数用于获取一个字符串的长度,返回值为整数类型,表示字符串中字符的数量。该函数不会计算字符串中的空格或其他特殊字符,只计算字符本身的数量。

例如,如果有一个字符串 $str = "hello world";,则使用 strlen() 函数可以获取它的长度:echo strlen($str); 将输出 11,因为字符串中有 11 个字符,包括空格。

<?php
$str = 'abcdef';
echo strlen($str); // 6

$str = ' ab cd ';
echo strlen($str); // 7
?>

mb_strlen — 获取字符串的长度。

mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )

返回具有 encoding 编码的字符串 str 包含的字符数。 多字节的字符被计为 1。如果给定的 encoding 无效则返回 FALSE

str_word_count — 返回字符串中单词的使用情况。

函数用于计算一个字符串中单词的数量。该函数默认会将所有连续的字母字符视为一个单词,并返回单词的数量。如果需要统计非字母字符,可以使用第二个参数来指定统计方式,例如 str_word_count($str, 1) 将返回一个数组,其中包含字符串中所有单词的位置和长度信息。

例如,如果有一个字符串 $str = "hello world";,则使用 str_word_count() 函数可以获取它的单词数量:echo str_word_count($str); 将输出 2,因为字符串中只有两个单词。

如果使用 str_word_count($str, 1),则可以获取每个单词的位置和长度:print_r(str_word_count($str, 1)); 将输出 Array ( [0] => 0 [1] => 5 [2] => 6 [3] => 11 ),其中第一个元素表示第一个单词的位置和长度,第二个元素表示第二个单词的位置和长度。

总之,strlen() 、mb_strlen()和 str_word_count() 函数都是字符串处理函数,用于获取字符串的长度和单词数量。根据具体的需求,可以选择使用不同的函数来处理字符串。

以上,其实还是等于没写,没有说清楚,

WordPress函数:add_action(添加动作)用法

将函数连接到指定action(动作)。

Plugin API/Action Reference 上查看动作hook列表。wordpress核心调用do_action() 时触发动作。

用法:

<?php
   add_action( $tag, $function_to_add, $priority,
         $accepted_args );
?> 

参数:

$tag

(字符串)(必填)$function_to_add  所挂载的动作(action)的名称。(在Plugin API/Action Reference 上查看动作hook列表)。也可以是一个主题或插件文件内部的一个动作,或者特定的标签“all”,这个函数将被所有的钩子(hooks)调用。

默认值:None

$function_to_add

(回调)(必填)你希望挂载的函数的名称。注:在 PHP“回调”类型文档中 所罗列的字符串格式化的语法均可用。

默认值:None

$priority

(整数)(可选)用于指定与特定的动作相关联的函数的执行顺序。数字越小,执行越早,具有相同优先级的函数在它们被添加到动作的顺序执行。

默认值:10

$accepted_args

(整数)(可选)挂钩函数所接受的参数数量。在 WordPress1.5.1 及以后的版本中,挂钩函数可以是调用do_action() 或 apply_filters()时设置的参数。例如,comment_id_not_found动作将传递任何函数,若该函数将所请求的评论编号连接到该动作。

默认值:1

返回值

(布尔)总是True。


示例:博客发表新内容时用电子邮件通知朋友:

function email_friends( $post_ID )  
{
   $friends = 'bob@example.org, susie@example.org';
   wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );
 
   return $post_ID;
}
add_action( 'publish_post', 'email_friends' );

接受的参数

挂钩函数可以选择接受从动作调用的参数,如果有任何要传递的话。在这个简单的例子中,echo_comment_id  函数需要 $comment_id 参数,该参数将在 comment_id_not_found 过滤钩子运行时通过 do_action() 传递。

function echo_comment_id( $comment_id ) 
{
   echo 'Comment ID ' . $comment_id . ' could not be found';
}
add_action( 'comment_id_not_found', 'echo_comment_id', 10, 1 );

注释

要找出一个动作的参数的ID和名称,只需搜索匹配 do_action() 调用的代码库。举例来说,如果你挂载到’save_post’,你会在 post.php 找到:

<?php do_action( 'save_post', $post_ID, $post ); ?>

你的 add_action 调用将是这样:

<?php add_action( 'save_post', 'my_save_post', 10, 2 ); ?>

而且你的函数将是这样:

function my_save_post( $post_ID, $post )
{
   // do stuff here
}

在一个类中使用 add_action

当你的插件或主题使用类来创建时,使用 add_action 钩子,在类中添加 $this 和 函数名称 到你的 add_action 回调,像这样:

class MyPluginClass
{
    public function __construct()
    {
         //add your actions to the constructor!
         add_action( 'save_post', array( $this, 'myplugin_save_posts' ) );
    }
    
    public function myplugin_save_posts()
    {
         //do stuff here...
    }

源文件

add_action() 位于 wp-includes/plugin.php

❌