WordPress文末加版权声明:3步代码+防搬运实战
WordPress文章末尾自动追加版权声明的完整方案:the_content过滤器优先级20+esc_url和esc_html安全转义+is_singular精准判定,配套CSS暗色模式适配,附copy事件防搬运JS(不影响代码块和表单),并打包成mu-plugins独立插件方便多站复用。含Typecho/Z-Blog/Hexo/Hugo/Discuz/Ghost跨平台迁移版本。
仅对 XML MIME 生效)。 if (preg_match('#\.(xml|xsl|txt|rss|json|atom)$#i', $path)) return false; // 后台数据不缓存 $pattern = '#^' . __TYPECHO_ADMIN_DIR__ . '#i'; if (preg_match($pattern, $path)) return false; // action动作不缓存 $pattern = '#^/action#i'; if (preg_match($pattern, $path)) return false; // fix:pjax search 失效 $requestUrl = self::$request->getRequestUri(); // search 请求第一次不缓存 $pattern = '/.*?s=.*/i'; if (preg_match($pattern, $requestUrl)) return false; // search 重定向后可以缓存 $pattern = '#^/search#i'; if (preg_match($pattern, $path) and in_array('search', self::$plugin_config->cache_page)) return true; $_routingTable = self::$sys_config->routingTable; // 针对文章页做特殊处理,付费文章不缓存 $post_regx = $_routingTable[0]['post']['regx']; if (preg_match($post_regx,$path,$arr)){ /** * 付费文章不缓存 * [0] => /archives/377.html * [1] => 377 */ if ($arr[1] and !empty($arr[1])){ // 查看文章是否是 tepass 付费文章 $db = Typecho_Db::get(); try { // fix: 版本不同导致读取数据库名错误 @zsuroy $database = isset($db->getConfig($db::READ)['database']) ? $db->getConfig($db::READ)['database'] : $db->getConfig($db::READ)[0]->database; $tepass_exist = $db->fetchRow($db->select()->from('information_schema.TABLES')->where('TABLE_NAME = ?',$db->getPrefix().'tepass_posts')->where('TABLE_SCHEMA = ?',$database)); if (isset($tepass_exist) and count($tepass_exist) > 0){ $p_id = @$db->fetchObject($db->select('id')->from('table.tepass_posts')->where('post_id = ?',$arr[1]))->id; if ($p_id) return false; } }catch (Typecho_Db_Query_Exception $e){ // 没有tepass } } } foreach ($_routingTable[0] as $page => $route) { if ($route['widget'] != 'Widget_Archive' && $route['widget'] != '\Widget\Archive') continue; if (preg_match($route['regx'], $path)) { $exclude = array('_year', '_month', '_day', '_page'); $page = str_replace($exclude, '', $page); if (in_array($page, self::$plugin_config->cache_page)) return true; } } return false; } public static function post_update($contents, $class) { if ('publish' != $contents['visibility'] || $contents['created'] > time()) return; self::initEnv(); if (self::$plugin_config->cache_driver == '0') return; self::$passed = true; $type = $contents['type']; $routeExists = (NULL != Typecho_Router::get($type)); if (!$routeExists) { self::initPath('#'); self::delCache(self::$path); return; } $db = Typecho_Db::get(); $contents['cid'] = $class->cid; $contents['categories'] = $db->fetchAll($db->select()->from('table.metas') ->join('table.relationships', 'table.relationships.mid = table.metas.mid') ->where('table.relationships.cid = ?', $contents['cid']) ->where('table.metas.type = ?', 'category') ->order('table.metas.order', Typecho_Db::SORT_ASC)); $contents['category'] = urlencode(current(Typecho_Common::arrayFlatten($contents['categories'], 'slug'))); $contents['slug'] = urlencode(empty($contents['slug'])?$class->slug:$contents['slug']); $contents['date'] = new Typecho_Date($contents['created']); $contents['year'] = $contents['date']->year; $contents['month'] = $contents['date']->month; $contents['day'] = $contents['date']->day; if (!self::initPath(Typecho_Router::url($type, $contents))){ return; // throw new Typecho_Exception('初始化失败。url info:'.Typecho_Router::url($type, $contents)); } self::delCache(self::$path); // 同时,删除 markdown 的部分缓存 if ($class->cid) self::delCache(self::getPostMarkCacheName($class->cid)); } public static function post_del_update($cid, $obj) { $db = Typecho_Db::get(); $postObject = $db->fetchObject($db->select('cid','slug', 'type') ->from('table.contents')->where('cid = ?', $cid)); if (!$postObject->cid){ return; } // 生成path $value = []; $value['cid'] = $cid; $value['type'] = $postObject->type; $value['slug'] = urlencode($postObject->slug); $pathInfo = Typecho_Router::url($value['type'], $value); self::initEnv(); self::initBackend(self::$plugin_config->cache_driver); self::delCache($pathInfo); if ($cid){ self::delCache(self::getPostMarkCacheName($cid)); } } public static function comment_update($comment) { self::initEnv(); if (!self::preCheck(false)) return; if (!self::initBackend(self::$plugin_config->cache_driver)) return; // 获取评论的PATH_INFO $path_info = self::$request->getInstance()->getPathInfo(); // 删除最后的 /comment就是需删除的path $article_url = preg_replace('/\/comment$/i','',$path_info); self::delCache($article_url); // 同时,删除 markdown 的部分缓存 if ($comment->cid) self::delCache(self::getPostMarkCacheName($comment->cid)); } public static function comment_update2($comment, $edit) { self::initEnv(); self::preCheck(false); self::initBackend(self::$plugin_config->cache_driver); $perm = stripslashes($edit->parentContent['permalink']); $perm = preg_replace('/(https?):\/\//', '', $perm); $perm = preg_replace('/'.$_SERVER['HTTP_HOST'].'/', '', $perm); self::delCache($perm); // 同时,删除 markdown 的部分缓存 if ($edit->cid) self::delCache(self::getPostMarkCacheName($edit->cid)); if ($comment['cid']) self::delCache(self::getPostMarkCacheName($comment['cid'])); } // 缓存content public static function getPostMarkCacheName($cid){ if (!self::$path) self::$path = self::$request->getInstance()->getPathInfo(); return self::$path.'_'.$cid.'_markdown'; } public static function cache_contentEx($content, $obj, $lastResult){ $content = empty( $lastResult ) ? $content : $lastResult; if (self::$plugin_config->enable_markcache == '0'){ return $content; } // 文章页面设置了标记的就不缓存 if (substr_count($content,'')) return $content; // 为 content 设置特殊的 cache name self::$path = self::$request->getInstance()->getPathInfo(); $cacheName = self::getPostMarkCacheName($obj->cid); self::initEnv(); if (!self::preCheck(false)) { return $content; } if (!self::initBackend(self::$plugin_config->cache_driver)){ return $content; } // 获取评论的PATH_INFO // $requestUrl = self::$request->getRequestUri(); try { // 获取当前url的缓存 $data = self::getCache($cacheName); if (!empty($data)) { //缓存未过期, 跳过之后的缓存重写入 if ($data['time'] + self::$plugin_config->expire < time()) self::$passed = false; return $data['html']; } } catch (Exception $e) { // echo $e->getMessage(); return $content; } // 没有缓存就缓存起来 if (is_null(self::$path) || !self::$passed) return $content; $data = array(); $data['time'] = time(); $data['html'] = $content; self::setCache($data,$cacheName); return $content; } public static function forTpCacheToolbar(){ ?>
标签
保哥笔记「WordPress版权」标签收录 2 篇相关内容,含《WordPress文末加版权声明:3步代码+防搬运实》《WordPress去除底部版权5种方法完整指南》等,提供 SEO/GEO 视角的深度解析。
WordPress文章末尾自动追加版权声明的完整方案:the_content过滤器优先级20+esc_url和esc_html安全转义+is_singular精准判定,配套CSS暗色模式适配,附copy事件防搬运JS(不影响代码块和表单),并打包成mu-plugins独立插件方便多站复用。含Typecho/Z-Blog/Hexo/Hugo/Discuz/Ghost跨平台迁移版本。
WordPress底部自豪地采用版权信息位于site-info.php,可通过删除printf代码、子主题覆盖、gettext过滤器、customize面板或FSE编辑器五种方式安全去除,本文按主题流派给出推荐方案与踩坑记录。