code snippet disappears only plain text remains in tinymce

旧巷老猫 提交于 2019-12-25 07:14:44

问题


Everytime i insert code snippet in timymce editor and plublish the article. It looks alright. But if i go to edit the article again most of my code is disappeard and only text remains. I am using it with laravel framework.

for example i put this code snippet in my article and publish

Here is the full code that you can copy paste. This is my article title.
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
 <head>
 <meta charset="<?php bloginfo('charset' ); ?>">
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
 <meta name="description" content="<?php bloginfo('description'); ?>">

 <title><?php bloginfo('name'); ?> | <?php is_front_page() ? bloginfo('description') : wp_title(); ?><?php wp_title(); ?></title>

 <!-- Custom styles for this template -->
 <link href="<?php bloginfo('template_url'); ?>/css/bootstrap.css" rel="stylesheet">
 <link href="<?php bloginfo('stylesheet_url'); ?>" rel="stylesheet">
 <link href="/css/font-awesome.css" rel="stylesheet">
 <?php wp_head(); ?>
 </head>

 <body>

 <div class="blog-masthead">
 <div class="container">
 <nav class="blog-nav">
 <a class="blog-nav-item active" href="#">Home</a>
 <a class="blog-nav-item" href="#">New features</a>
 <a class="blog-nav-item" href="#">Press</a>
 <a class="blog-nav-item" href="#">New hires</a>
 <a class="blog-nav-item" href="#">About</a>
 </nav>
 </div>
 </div>

Now it will publish alright. But if i go to edit that article again all i see is this bit of code: Once i click edit all my previous code block is gone with only text left.

<pre class="language-markup"><code>
&gt;
 
 
 
 
 

 &lt;?php bloginfo('name'); ?&gt; | &lt;?php is_front_page() ? bloginfo('description') : wp_title(); ?&gt;&lt;?php wp_title(); ?&gt;

 
 
 
 
 
 

 
 Home
 New features
 Press
 New hires
 About
 
 
 </code></pre>

As you can see, both of the code blocks supposed to appear same but thats not happening.

Here is my editor config for tinymce:

<script src="http://code.jquery.com/jquery.js"></script>
<script src="//cdn.tinymce.com/4/tinymce.min.js"></script>
<script>
      var editor_config = {
        path_absolute : "{{ URL::to('/') }}/",
        selector: "textarea",
        //entity_encoding: "raw",
        // encoding: 'xml',
        plugins: [
          "advlist autolink lists link image charmap print preview hr anchor pagebreak",
          "searchreplace wordcount visualblocks visualchars code fullscreen",
          "insertdatetime media nonbreaking save table contextmenu directionality",
          "emoticons template paste textcolor colorpicker textpattern",
          "codesample",
          "spellchecker"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media | spellchecker | codesample",
        menubar: "tools",
        spellchecker_callback: function(method, text, success, failure) {
            var words = text.match(this.getWordCharPattern());
            if (method == "spellcheck") {
              var suggestions = {};
              for (var i = 0; i < words.length; i++) {
                suggestions[words[i]] = ["First", "Second"];
              }
              success(suggestions);
            }
          },
        relative_urls: false,
        file_browser_callback : function(field_name, url, type, win) {
          var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
          var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;

          var cmsURL = editor_config.path_absolute + 'laravel-filemanager?field_name=' + field_name;
          if (type == 'image') {
            cmsURL = cmsURL + "&type=Images";
          } else {
            cmsURL = cmsURL + "&type=Files";
          }

          tinyMCE.activeEditor.windowManager.open({
            file : cmsURL,
            title : 'Filemanager',
            width : x * 0.8,
            height : y * 0.8,
            resizable : "yes",
            close_previous : "no"
          });
        }
      };

      tinymce.init(editor_config);
    <!--   -->
</script>

I spend hours searching everywhere but no success. Your help would be highly appriciated. Probably I am missing something in config. I want to create blog posts using tinymce with laravel and will be using a lot of code snippets wrapped in pre/code. Thank you.

Here is my create page textarea and edit page textarea, exactely same: and i have included the tinymce script on both page, the code is what I have posted above.

<div class="form-group">
     {!! Form::label("body", "Body:") !!}
     {!! Form::textarea("body", null, ['class' => 'form-control']) !!}  
 </div>

Here is my article create controller:

public function store(Request $request)
    {
        $this->validate($request, $rules);

        $input = $request->all();
        $input['slug'] = str_slug($request->title);
        $input['user_id'] = Auth::user()->id;
        $input['meta_title'] = $request->title;
        if ($file = $request->file('photo_id')) {
            $name = Carbon::now(). '.' .$file->getClientOriginalName();
            $file->move('images', $name);
            $photo = Photo::create(['photo' => $name]);
            $input['photo_id'] = $photo->id;
        }
        $article = Article::create($input);
        if ($categoryIds = $request->category_id) {
            $article->category()->sync($categoryIds);
        }
        notify()->flash('<h3>New article has been created successfully</h3>', 'success', ['timer' => 2000]);
                return redirect()->back()->withInput();
    }

Here is my article update controller:

public function update(Request $request, $id)
    {
        $this->validate($request, $rules);
        $article = Article::findOrFail($id);
        $input['slug'] = str_slug($request->title);
        $input['meta_title'] = $request->title;
        $input = $request->all();
        if ($file = $request->file('photo_id')) {
            if ($article->photo) {
            unlink('images/' . $article->photo->photo);
            $article->photo()->delete('photo');
            }
            $name = Carbon::now(). '.' .$file->getClientOriginalName();
            $file->move('images', $name);
            $photo = Photo::create(['photo' => $name]);
            $input['photo_id'] = $photo->id;
        }
        $article->update($input);
        if ($categoryIds = $request->category_id) {
            $article->category()->sync($categoryIds);
        }
        notify()->flash('<h3>You have successfully edit the article.</h3>', 'success', ['timer' => 2000]);
        return redirect()->back()->withInput();
    }

来源:https://stackoverflow.com/questions/39411304/code-snippet-disappears-only-plain-text-remains-in-tinymce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!