【问题标题】:wp_insert_post() altering double quoteswp_insert_post() 改变双引号
【发布时间】:2018-06-09 13:36:08
【问题描述】:

我正在尝试使用 wp_insert_post() 将脚本上传到 Wordpress,但是一旦它到达服务器,引号就会在几个地方更改为 ”,这可能解释了为什么我似乎无法得到它在职的。我尝试过使用 sanitize() 函数,但这会通过在所有行的末尾插入 </p> 标记来搞乱一切,所以这不是一个真正的选择。

我在这里读到Escape double quotes within double quotes 连接的字符串(我正在使用,以及heredoc 字符串)可能是问题的原因,但我不确定如何解决这个问题。欢迎所有建议 - 不幸的是,我尝试上传的 jQuery 脚本是一个由用户通过 WP-Contact7/Flamingo 上传到 Wordpress 的图像构建的图库。

或者简单地转义所有引号是否有效?但是那些只需要连接起来的字符串呢?喜欢<img src="' . $someImgUrl . '...';

这是我尝试上传的四个结构相似的字符串之一...

                        $gallBd2A = <<< gallBd2A
                                </div><!-- id="rundgang"-->

                                    <div id="gallKnopfeTeil">
                                        <input type="button" id="bildZuKnopf" name="1" class="gallKnopfe" value=" >>> " onClick="bildZu(this)">
                                        <input type="button" id="bildAbKnopf" name="1" class="gallKnopfe" value=" <<< " onClick="bildAb(this)">
                                    </div><!-- id="gallKnopfeTeil" -->

                                <script src='./jquery.js'>
                                </script>


                                <script>
                                    function probeZufug(bd){
                                        var e = document.getElementById('probeKaste');
                                        e.innerHTML += "<br/>" + bd;
                                    }
                                </script>


                                <script>
                                        //  if (plusZiff == 7)
                                        //  jzZiff < 6? jzZiff++: jzZiff=1;
                                        //  if (minderZiff == 0) { minderZiff = 6;}
                                        //  jzZiff > 1? jzZiff--: jzZiff=6;

                                            // jQuery('#bild1').fadeOut(100);
                                            jQuery('#bild2').fadeOut(100);
                                            jQuery('#bild3').fadeOut(100);
                                            jQuery('#bild4').fadeOut(100);
                                            jQuery('#bild5').fadeOut(100);
                                            jQuery('#bild6').fadeOut(100);
                                            jQuery('#bild7').fadeOut(100);
                                            jQuery('#bild8').fadeOut(100);

                                        function bildZu(ds){
                                            var jzZiff = parseInt(ds.name);
                                            var plusZiff = jzZiff+1;

gallBd2A;

然后在四个这样的字符串之后,我将它们连接起来:

$gzGallBd = $gallKaskBd . $gallBd1 . $gallBdM . $gallBd2A . $gallBd2B . $gallBd2C . $gallBd2D . $gallBd2L;

这个字符串然后被添加到一个 post-object,其对象变量作为 post-array 的参数传递给 wp_insert_post()。

该脚本在用于上传它的页面上输出时运行良好(作为检查),如果我将其复制并粘贴回不在 wordpress 内的本地页面,则上传的代码可以正常工作。

但在 wordpress 帖子中,我将图片并排显示,就好像没有 jQuery 脚本隐藏它们一样。直到我调查帖子并单击以将其保存为可视模式,此时图像按预期堆叠,但没有任何按钮功能,因为 onClick(..) 函数已从 &lt;input type="button"...&gt; 元素中消失。

如有必要,我会尽快发布完整的代码,同时非常非常欢迎任何反馈。

【问题讨论】:

    标签: jquery wordpress upload


    【解决方案1】:

    您是否使用以下方法插入帖子:

    $user_id = get_current_user_id(); $someImgUrl = "https://cdn.pixabay.com/photo/2016/03/26/13/09/organic-1280537_960_720.jpg"; $new_post = 数组( 'ID' => '', 'post_author' => $user_id, 'post_category' => '', 'post_content' => '', 'post_title' => '自定义帖子', 'post_status' => '发布' ); $post_id = wp_insert_post($new_post);

    使用这种方法,我的帖子内容中出现了双引号。如果没有得到解决,请详细说明您的问题。

    【讨论】:

    • 是的,我正在使用 wp_insert_post() 并且其他所有内容都可以正常上传 - post_title、post_status、其余内容等......只有这个动画被证明是棘手的。不知道为什么它只转换某些双引号???
    • 好的,我明白了..您在 jquery srcipt 中遇到问题,即 OnClick 功能不起作用。分享您的脚本或完整代码,以便我进行测试。
    • 感谢您的提议,最后我将代码更改为仅使用一个单引号字符串,其中的单引号已转义。我也遇到了 >>> 的问题,它显然关闭了 wordpress 或其他东西 中的一些标签。请参阅答案以获得进一步说明。
    • 是的,谢谢,终于!猜猜你有时只需要以正确的方式做事......
    【解决方案2】:

    好吧,既然我最初的问题没有得到解答,我会为任何有同样问题的人提出解决方案:

    我重写了上面的原始脚本,将所有内容放在单引号字符串中,任何单引号都带有转义符,并省略了 heredocs,因为结束的 heredoc 标记需要位于换行符的开头,这可能会也可能不会当字符串被传递时成为问题。

    完整的 jquery/javascript 功能以 wordpress 的文件结构放入其自己的 .js 文件中并包含在

    function eigenSchrifte() {
        wp_enqueue_script( 'schleicher1', get_template_directory_uri() . '/js/schleicher1.js', array( 'jquery' ), '1.12.4', true );
    }
    add_action( 'wp_enqueue_scripts', 'eigenSchrifte' );
    

    我最终将特定于画廊的 .css 放入主题的 single.php 页面,因为滑块只会显示在单个帖子上。

    剩下的动态生成的html带有&lt;img src="..."&gt;标签,而用于构建画廊的php脚本现在只包含html,它上传得非常好,尽管没有&amp;gt;&amp;gt;&amp;gt;甚至&amp;gt;&amp;gt;&amp;gt;作为按钮文本.

    最终的 html 构造的粗略近似是这样运行的(本身并不那么有启发性,但对于与原始尝试进行比较很有用):

    $galleryString1 = '<div id="carousel">';
    
    $dynamicGalleryString = '';
    
    
    foreach ($imageArray as $targetImage) {
            $targetImage++;
            $dynamicGalleryString .= find_and_copy_target_uploaded_image_returns_full_image_tag($targetImage, $newFolder);
    
    } //
    
    
    $galleryString2 =                               
            '</div><!-- id="carousel"-->
    
                <div id="galleryButtonDiv">
                    <input type="button" id="nxPicButton" name="1" class="galleryButton" value=" ++ " onClick="nextPic(this)">
                    <input type="button" id="prevPicButton" name="1" class="galleryButton" value=" -- " onClick="prevPic(this)">
                </div><!-- id="galleryButtonDiv" -->
            ';
    

    【讨论】:

      猜你喜欢
      • 2010-09-20
      • 2013-05-26
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多